Skip to content

Commit ec75cf3

Browse files
committed
feat: Implement Trie
1 parent 4b11e52 commit ec75cf3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 시간복잡도: O(n) (n은 문자열의 길이)
2+
# 공간복잡도: O(n) (n은 문자열의 길이)
3+
class Trie:
4+
def __init__(self):
5+
self.root = {}
6+
self.end = False
7+
8+
def insert(self, word: str) -> None:
9+
node = self.root
10+
for char in word:
11+
if char not in node:
12+
node[char] = {}
13+
node = node[char]
14+
node[self.end] = True
15+
16+
def search(self, word: str) -> bool:
17+
node = self.root
18+
for char in word:
19+
if char not in node:
20+
return False
21+
node = node[char]
22+
return self.end in node
23+
24+
def startsWith(self, prefix: str) -> bool:
25+
node = self.root
26+
for char in prefix:
27+
if char not in node:
28+
return False
29+
node = node[char]
30+
return True
31+

0 commit comments

Comments
 (0)