Week 3-2. [Trie] 208. Implement Trie (Prefix Tree) (Python3)
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋ฌธ์ ์์ฝ
https://leetcode.com/problems/implement-trie-prefix-tree/?envType=study-plan-v2&envId=top-interview-150
Implement Trie (Prefix Tree) - LeetCode
Can you solve this real interview question? Implement Trie (Prefix Tree) - A trie [https://en.wikipedia.org/wiki/Trie] (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There
leetcode.com
โก๏ธ insert, search, startsWith ๋ฉ์๋๋ฅผ ๊ฐ์ง๋ Trie ์๋ฃ ๊ตฌ์กฐ๋ฅผ ์ง์ ๊ตฌํํ๋ผ.
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
insert()
๋ฃจํธ ๋ ธ๋๋ถํฐ ํ๋์ฉ ์์ ๋ ธ๋๋ฅผ ์ํํ๋ฉด์ word์ ๊ตฌ์ฑ ์์๊ฐ ์์ ๋๋ง๋ค ์๋ก์ด ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ค.
search()
๋ง์ฐฌ๊ฐ์ง๋ก ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ ๋ ธ๋๋ฅผ ์ํํ๋๋ฐ, ์ด๋ฒ์๋ word์ ๊ตฌ์ฑ ์์๊ฐ ์์ ๋ ๋ฐ๋ก False๋ฅผ ๋ฐํํ๋ค.
startsWith()
๋ง์ฐฌ๊ฐ์ง๋ก ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ ๋ ธ๋๋ฅผ ์ํํ๋๋ฐ, ์ด๋ฒ์๋ prefix์ ๊ตฌ์ฑ ์์๊ฐ ์์ ๋ ๋ฐ๋ก False๋ฅผ ๋ฐํํ๋ค.
๐ฏ ํ์ด
์ฌ๊ท๋ฅผ ์ด์ฉํ ํ์ด์ด๋ค.
class Node:
def __init__(self):
self.children = dict()
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
self._insert(self.root, word)
def _insert(self, node: Node, word: str) -> None:
if len(word) == 0:
node.isEndOfWord = True
return
c = word[0]
child = node.children.get(c)
if child is None:
node.children[c] = Node() # ์๋ก์ด ๋
ธ๋ ์ถ๊ฐ
child = node.children.get(c)
self._insert(child, word[1:])
def search(self, word: str) -> bool:
return self._search(self.root, word)
def _search(self, node: Node, word: str) -> bool:
if len(word) == 0:
return node.isEndOfWord
c = word[0]
child = node.children.get(c)
if child is None:
return False
return self._search(child, word[1:])
def startsWith(self, prefix: str) -> bool:
node = self.root
for x in prefix:
child = node.children.get(x)
if child == None: return False
node = child
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
์๋์ฒ๋ผ ์ฝ์ ํ ๋ ์ฌ๊ท๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌํํ ์๋ ์๋ค.
# ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ
def insert(self, word: str) -> None:
node = self.root
for c in word:
if not c in node.children:
node.children[c] = Node()
node = node.children[c]
node.isEndOfWord = True
์ ์ฝ๋์ ์๊ฐ ๋ณต์ก๋๋ ์ค์ ์ํ๋ฅผ ํตํด ์ด์ง ํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ฏ๋ก O(n)์ด๋ค.
๋ฌธ์ ์์ ์ ๋ ฅ์ ์ต๋๋ 10^4์ด๋ฏ๋ก ๊ฐ๋นํ ์ ์๋ ์ ๋์ด๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
์ฌ๊ท๋ฅผ ์ฌ์ฉํ์ง ์์ ๋์ฑ ์ฝ๊ณ ์ดํดํ๊ธฐ ์ข๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ์๊ฐ ๋ณต์ก๋๋ ์ด์ง ํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๋ฏ๋ก O(n)์ด๋ค.
class Node:
def __init__(self):
self.children = dict()
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
node = self.root
for c in word:
if c not in node.children:
node.children[c] = Node()
node = node.children[c]
node.isEndOfWord = True
def search(self, word: str) -> bool:
node = self.root
for c in word:
if c in node.children:
node = node.children[c]
else: return False
return node.isEndOfWord
def startsWith(self, prefix: str) -> bool:
node = self.root
for c in prefix:
if c in node.children:
node = node.children[c]
else: return False
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)