Skip to content

Commit 46cbe9f

Browse files
committed
add solution: implement-trie-prefix-tree
1 parent de95a18 commit 46cbe9f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
๋ฌธ์ œ: ํŠน์ • ๋ช…๋ น์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํŠธ๋ผ์ด(Trie) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.
3+
ํ’€์ด: ๋‹จ์ˆœํžˆ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋ผ์ด ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
4+
'''
5+
6+
7+
class Trie:
8+
9+
def __init__(self):
10+
self.arr = []
11+
12+
def insert(self, word: str) -> None:
13+
self.arr.append(word)
14+
15+
def search(self, word: str) -> bool:
16+
for i in self.arr:
17+
if i == word:
18+
return True
19+
return False
20+
21+
def startsWith(self, prefix: str) -> bool:
22+
n = len(prefix)
23+
for i in self.arr:
24+
if i[:n] == prefix:
25+
return True
26+
return False
27+
28+
29+
# Your Trie object will be instantiated and called as such:
30+
# obj = Trie()
31+
# obj.insert(word)
32+
# param_2 = obj.search(word)
33+
# param_3 = obj.startsWith(prefix)

0 commit comments

Comments
ย (0)