forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92c67d5
commit cdaf975
Showing
7 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> stringMatching(vector<string>& 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<string> res; | ||
for (string& word : words) { | ||
if (get(word)) res.emplace_back(word); | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution { | ||
public List<String> 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<String> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters