File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ class WordDictionary:
2+ """
3+ μκ³ λ¬λ μ μμμ λ³΄κ³ νλλ° μμ§ μ΄ν΄κ° μ λμ§ μμ.
4+ μΆκ°μ μΈ νμ΅ νμ
5+ """
6+ def __init__(self):
7+ self.root = {"$": True}
8+
9+ def addWord(self, word: str) -> None:
10+ node = self.root
11+ for ch in word:
12+ if ch not in node:
13+ node[ch] = {"$": False}
14+ node = node[ch]
15+ node["$"] = True
16+
17+ def search(self, word: str) -> bool:
18+ def dfs(node, idx):
19+ if idx == len(word):
20+ return node["$"]
21+ ch = word[idx]
22+ if ch in node:
23+ return dfs(node[ch], idx + 1)
24+ if ch == ".":
25+ if any(dfs(node[k], idx + 1) for k in node if k != "$"):
26+ return True
27+ return False
28+
29+ return dfs(self.root, 0)
30+
31+
32+ # Your WordDictionary object will be instantiated and called as such:
33+ # obj = WordDictionary()
34+ # obj.addWord(word)
35+ # param_2 = obj.search(word)
You canβt perform that action at this time.
0 commit comments