From cdaf9758e6e58726150e2001e297a3c447f37ec3 Mon Sep 17 00:00:00 2001 From: luliyucoordinate Date: Sat, 18 Apr 2020 16:47:27 +0800 Subject: [PATCH] add 1408 --- README.md | 3 +- src/1408-String-Matching-in-an-Array/1408.cpp | 43 +++++++++++++++++ src/1408-String-Matching-in-an-Array/1408.go | 46 +++++++++++++++++++ .../1408.java | 41 +++++++++++++++++ src/1408-String-Matching-in-an-Array/1408.js | 39 ++++++++++++++++ src/1408-String-Matching-in-an-Array/1408.py | 20 ++++++++ src/addProb.py | 8 ++-- 7 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 src/1408-String-Matching-in-an-Array/1408.cpp create mode 100644 src/1408-String-Matching-in-an-Array/1408.go create mode 100644 src/1408-String-Matching-in-an-Array/1408.java create mode 100644 src/1408-String-Matching-in-an-Array/1408.js create mode 100644 src/1408-String-Matching-in-an-Array/1408.py diff --git a/README.md b/README.md index 9fddc6ac..0f070af0 100644 --- a/README.md +++ b/README.md @@ -703,4 +703,5 @@ LeetCode |1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|c|[c++](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp)|[python](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py)|[go](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go)|[js](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js)|[java](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java)|Easy| |1404|[Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/)|c|[c++](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.cpp)|[python](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.py)|[go](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.go)|[js](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.js)|[java](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.java)|Medium| |1405|[Longest Happy String](https://leetcode.com/problems/longest-happy-string/)|c|[c++](./src/1405-Longest-Happy-String/1405.cpp)|[python](./src/1405-Longest-Happy-String/1405.py)|[go](./src/1405-Longest-Happy-String/1405.go)|[js](./src/1405-Longest-Happy-String/1405.js)|[java](./src/1405-Longest-Happy-String/1405.java)|Medium| -|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard| \ No newline at end of file +|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard| +|1408|[String Matching in an Array](String Matching in an Array)|c|[c++](./src/1408-String-Matching-in-an-Array/1408.cpp)|[python](./src/1408-String-Matching-in-an-Array/1408.py)|[go](./src/1408-String-Matching-in-an-Array/1408.go)|[js](./src/1408-String-Matching-in-an-Array/1408.js)|[java](./src/1408-String-Matching-in-an-Array/1408.java)|Easy| \ No newline at end of file diff --git a/src/1408-String-Matching-in-an-Array/1408.cpp b/src/1408-String-Matching-in-an-Array/1408.cpp new file mode 100644 index 00000000..38751272 --- /dev/null +++ b/src/1408-String-Matching-in-an-Array/1408.cpp @@ -0,0 +1,43 @@ +const int M = 50010; +int son[M][27], nodes[M], idx; + +void insert(string str) { + int p = 0; + for (char c : str) { + int& s = son[p][c - 'a']; + if (!s) s = ++idx; + son[s][26]++; + p = s; + } +} + +bool get(string& str) { + int p = 0, s; + for (char c : str) { + s = son[p][c - 'a']; + if (!s) return false; + p = s; + } + return son[s][26] > 1; +} + +class Solution { +public: + vector stringMatching(vector& words) { + memset(son, 0, sizeof son); + memset(nodes, 0, sizeof nodes); + idx = 0; + + for (string& word : words) { + for (int i = 0; i < word.size(); i++) { + insert(word.substr(i)); + } + } + + vector res; + for (string& word : words) { + if (get(word)) res.emplace_back(word); + } + return res; + } +}; \ No newline at end of file diff --git a/src/1408-String-Matching-in-an-Array/1408.go b/src/1408-String-Matching-in-an-Array/1408.go new file mode 100644 index 00000000..e0751d3f --- /dev/null +++ b/src/1408-String-Matching-in-an-Array/1408.go @@ -0,0 +1,46 @@ +func stringMatching(words []string) []string { + root := &Trie{} + + for _, word := range words { + for i := 0; i < len(word); i++ { + root.Insert(word[i:len(word)]) + } + } + + res := []string{} + for _, word := range words { + if root.Get(word) { + res = append(res, word) + } + } + return res +} + +type Trie struct { + val int + next [26]*Trie +} + +func (this *Trie) Insert(word string) { + cur := this + for i := 0; i < len(word); i++ { + j := word[i] - 'a' + if cur.next[j] == nil { + cur.next[j] = &Trie{} + } + cur = cur.next[j] + cur.val++ + } +} + +func (this *Trie) Get(word string) bool { + cur := this + for i := 0; i < len(word); i++ { + j := word[i] - 'a' + if cur.next[j] == nil { + return false + } + cur = cur.next[j] + } + return cur.val > 1 +} \ No newline at end of file diff --git a/src/1408-String-Matching-in-an-Array/1408.java b/src/1408-String-Matching-in-an-Array/1408.java new file mode 100644 index 00000000..8daf5329 --- /dev/null +++ b/src/1408-String-Matching-in-an-Array/1408.java @@ -0,0 +1,41 @@ +class Solution { + public List stringMatching(String[] words) { + Trie root = new Trie(); + for (String word : words) { + for (int i = 0; i < word.length(); i++) { + root.insert(word.substring(i)); + } + } + + List res = new ArrayList(); + for (String word : words) { + if (root.get(word)) res.add(word); + } + return res; + } +} + +class Trie { + public int val; + public Trie[] next = new Trie[26]; + + public void insert(String str) { + Trie cur = this; + for (int i = 0; i < str.length(); i++) { + int j = str.charAt(i) - 'a'; + if (cur.next[j] == null) cur.next[j] = new Trie(); + cur = cur.next[j]; + cur.val++; + } + } + + public boolean get(String str) { + Trie cur = this; + for (int i = 0; i < str.length(); i++) { + int j = str.charAt(i) - 'a'; + if (cur.next[j] == null) return false; + cur = cur.next[j]; + } + return cur.val > 1; + } +} \ No newline at end of file diff --git a/src/1408-String-Matching-in-an-Array/1408.js b/src/1408-String-Matching-in-an-Array/1408.js new file mode 100644 index 00000000..45971114 --- /dev/null +++ b/src/1408-String-Matching-in-an-Array/1408.js @@ -0,0 +1,39 @@ +var stringMatching = function(words) { + let root = new Trie(); + for (let word of words) { + for (let i = 0; i < word.length; i++) { + root.insert(word.substring(i)); + } + } + + let res = []; + for (let word of words) { + if (root.get(word)) res.push(word); + } + return res; +}; + +var Trie = function() { + this.val = 0; + this.next = {}; +}; + +Trie.prototype.insert = function(word) { + let cur = this; + for (let c of word) { + if (!cur.next[c]) { + cur.next[c] = new Trie(); + } + cur = cur.next[c]; + cur.val++; + } +}; + +Trie.prototype.get = function(word) { + let cur = this; + for (let c of word) { + if (!cur.next[c]) return false; + cur = cur.next[c]; + } + return cur.val > 1; +} \ No newline at end of file diff --git a/src/1408-String-Matching-in-an-Array/1408.py b/src/1408-String-Matching-in-an-Array/1408.py new file mode 100644 index 00000000..2a40f848 --- /dev/null +++ b/src/1408-String-Matching-in-an-Array/1408.py @@ -0,0 +1,20 @@ +class Solution: + def stringMatching(self, words: List[str]) -> List[str]: + def add(word): + node = root + for c in word: + node = node[c] + node['#'] = node.get('#', 0) + 1 + + def get(word): + node = root + for c in word: + if (node := node.get(c)) is None: return False + return node['#'] > 1 + + Trie = lambda: collections.defaultdict(Trie) + root = Trie() + for word in words: + for i in range(len(word)): + add(word[i:]) + return [word for word in words if get(word)] \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index f9f4ee60..d8822d72 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -2,10 +2,10 @@ import os, bisect # 题目名称 -name = "Stone Game III" -ID = 1406 -url = "https://leetcode.com/problems/stone-game-iii/" -difficult = "Hard" +name = "String Matching in an Array" +ID = 1408 +url = "https://leetcode.com/problems/string-matching-in-an-array/" +difficult = "Easy" prog = ['c', 'cpp', 'py', 'go', 'js', 'java']