forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphaorderly.py
More file actions
38 lines (29 loc) · 1.46 KB
/
Copy pathalphaorderly.py
File metadata and controls
38 lines (29 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Time Complexity: O(L), where L is the length of the input string (word or prefix) per operation.
Space Complexity: O(N), where N is the total number of characters inserted into the trie (all words inserted).
- Each Trie node uses a dictionary to store children nodes for each character.
- The `end` flag indicates whether the current node represents the end of a complete word.
- The `insert` method adds a word to the trie by sequentially creating/following nodes for each character.
- The `search` method checks for the presence of a full word in the trie by traversing nodes for each character and checking the `end` flag at the last character.
- The `startsWith` method checks if there exists any word in the trie with the given prefix by traversing nodes for each character in the prefix.
"""
class Trie:
def __init__(self):
self.children = dict()
self.end = False
def insert(self, word: str) -> None:
node = self
for ch in word:
if ch not in node.children:
node.children[ch] = Trie()
node = node.children[ch]
node.end = True
def search(self, word: str, isPrefix: bool = False) -> bool:
node = self
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return True if isPrefix else node.end
def startsWith(self, prefix: str) -> bool:
return self.search(word=prefix, isPrefix=True)