File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_word = False
5+
6+
class Trie:
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.root = TrieNode()
13+
14+
def insert(self, word: str) -> None:
15+
"""
16+
Inserts a word into the trie.
17+
"""
18+
cur_node = self.root
19+
for char in word:
20+
if char not in cur_node.children:
21+
cur_node.children[char] = TrieNode()
22+
cur_node = cur_node.children[char]
23+
cur_node.is_word = True
24+
25+
def search(self, word: str) -> bool:
26+
"""
27+
Returns if the word is in the trie.
28+
"""
29+
cur_node = self.root
30+
for char in word:
31+
if char not in cur_node.children:
32+
return False
33+
cur_node = cur_node.children[char]
34+
return cur_node.is_word
35+
36+
37+
def startsWith(self, prefix: str) -> bool:
38+
"""
39+
Returns if there is any word in the trie that starts with the given prefix.
40+
"""
41+
cur_node = self.root
42+
for char in prefix:
43+
if char not in cur_node.children:
44+
return False
45+
cur_node = cur_node.children[char]
46+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.child = {}
4+
self.value = 0
5+
6+
class MapSum:
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.root = TrieNode()
13+
self.char_map = {}
14+
15+
def insert(self, key: str, val: int) -> None:
16+
delta = val - self.char_map.get(key, 0)
17+
cur_node = self.root
18+
self.char_map[key] = val
19+
cur_node.value += delta
20+
for char in key:
21+
if char not in cur_node.child:
22+
cur_node.child[char] = TrieNode()
23+
cur_node = cur_node.child[char]
24+
cur_node.value += delta
25+
26+
def sum(self, prefix: str) -> int:
27+
cur_node = self.root
28+
for char in prefix:
29+
if char not in cur_node.child:
30+
return 0
31+
cur_node = cur_node.child[char]
32+
return cur_node.value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_word = False
5+
self.word = ""
6+
7+
class Trie:
8+
def __init__(self):
9+
self.root = TrieNode()
10+
11+
def insertWord(self, word):
12+
cur = self.root
13+
for char in word:
14+
if char not in cur.children:
15+
cur.children[char] = TrieNode()
16+
cur = cur.children[char]
17+
cur.is_word = True
18+
cur.word = word
19+
20+
def findWord(self, word):
21+
cur = self.root
22+
for char in word:
23+
if char in cur.children:
24+
cur = cur.children[char]
25+
if cur.is_word:
26+
return cur.word
27+
else:
28+
return ""
29+
return ""
30+
31+
class Solution:
32+
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
33+
trie = Trie()
34+
35+
for word in dictionary:
36+
trie.insertWord(word)
37+
38+
sentence = sentence.split(" ")
39+
40+
for i in range(len(sentence)):
41+
root_word = trie.findWord(sentence[i])
42+
if root_word != "":
43+
sentence[i] = root_word
44+
return " ".join(sentence)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_word = False
5+
6+
class WordDictionary:
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.root = TrieNode()
13+
14+
def addWord(self, word: str) -> None:
15+
cur = self.root
16+
for char in word:
17+
if char not in cur.children:
18+
cur.children[char] = TrieNode()
19+
cur = cur.children[char]
20+
cur.is_word = True
21+
22+
def search(self, word: str) -> bool:
23+
def searchRec(i, trie):
24+
if i == len(word):
25+
return trie.is_word
26+
27+
# search all children if current char is "."
28+
if word[i] == ".":
29+
for val in trie.children.values():
30+
if searchRec(i + 1, val):
31+
return True
32+
return False
33+
34+
if word[i] not in trie.children:
35+
return False
36+
37+
return searchRec(i + 1, trie.children[word[i]])
38+
39+
return searchRec(0, self.root)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution:
2+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
3+
if not board or not board[0] or not words: return []
4+
5+
trie = Trie(words)
6+
7+
self.result = set()
8+
for r in range(len(board)):
9+
for c in range(len(board[0])):
10+
self.dfs(r, c, trie.root, "", board)
11+
12+
return list(self.result)
13+
14+
def dfs(self, r, c, trie, s, board):
15+
if trie.is_word:
16+
self.result.add(s)
17+
trie.is_word = False
18+
19+
if self.isValidPos(r, c, board):
20+
char = board[r][c]
21+
child_node = trie.children.get(char)
22+
if child_node is not None:
23+
s += char
24+
board[r][c] = None
25+
26+
self.dfs(r + 1, c, child_node, s, board)
27+
self.dfs(r - 1, c, child_node, s, board)
28+
self.dfs(r, c + 1, child_node, s, board)
29+
self.dfs(r, c - 1, child_node, s, board)
30+
31+
board[r][c] = char
32+
33+
def isValidPos(self, r, c, board):
34+
if not 0 <= r < len(board):
35+
return False
36+
if not 0 <= c < len(board[0]):
37+
return False
38+
if board[r][c] is None:
39+
return False
40+
return True
41+
42+
class TrieNode:
43+
def __init__(self):
44+
self.children = {}
45+
self.is_word = False
46+
47+
class Trie:
48+
def __init__(self, words):
49+
"""
50+
Initialize your data structure here.
51+
"""
52+
self.root = TrieNode()
53+
for word in words:
54+
self.insert(word)
55+
56+
def insert(self, word: str) -> None:
57+
"""
58+
Inserts a word into the trie.
59+
"""
60+
cur_node = self.root
61+
for char in word:
62+
if char not in cur_node.children:
63+
cur_node.children[char] = TrieNode()
64+
cur_node = cur_node.children[char]
65+
cur_node.is_word = True
Whitespace-only changes.

0 commit comments

Comments
 (0)