-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSolution.py
37 lines (28 loc) ยท 1.01 KB
/
Solution.py
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
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
current = self.root
for char in word:
if char not in current.children:
current.children[char] = Node()
current = current.children[char]
current.is_word = True
def search(self, word: str) -> bool:
current = self.root
for char in word:
if char not in current.children:
return False
current = current.children[char]
return current.is_word
def startsWith(self, prefix: str) -> bool:
current = self.root
for char in prefix:
if char not in current.children:
return False
current = current.children[char]
return True
class Node:
def __init__(self): # ์ธ์คํด์ค๋ง๋ค ๋ฌ๋ผ์ ธ์ผํ๋ฏ๋ก ์ด๊ธฐํ ํจ์์ ์์ฑ
self.children = {}
self.is_word = False # ๋ง์ง๋ง์์ ์์์ผ ํจ(๊ฒ์์ ํ์)