Skip to content

Commit 48f60ab

Browse files
committed
leetcode.com 211. Design Add and Search Words Data Structure
문제 링크: https://leetcode.com/problems/design-add-and-search-words-data-structure/
1 parent 2fe0de1 commit 48f60ab

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import Dict
2+
3+
4+
class Node(Dict):
5+
def __init__(self):
6+
self.ends = False
7+
8+
9+
class WordDictionary:
10+
def __init__(self):
11+
self.node = Node()
12+
13+
def addWord(self, word: str) -> None:
14+
node = self.node
15+
16+
for c in word:
17+
if c not in node.keys():
18+
node[c] = Node()
19+
20+
node = node[c]
21+
22+
node.ends = True
23+
24+
def search(self, word: str) -> bool:
25+
return self.mysearch(word, self.node)
26+
27+
def mysearch(self, word: str, node) -> bool:
28+
for idx in range(len(word)):
29+
c = word[idx]
30+
31+
if c == '.':
32+
rtn = False
33+
for my_c in node.keys():
34+
rtn |= self.mysearch(word[idx + 1:], node[my_c])
35+
36+
return rtn
37+
else:
38+
if c in node.keys():
39+
node = node[c]
40+
else:
41+
return False
42+
43+
return node.ends
44+
45+
# Your WordDictionary object will be instantiated and called as such:
46+
# obj = WordDictionary()
47+
# obj.addWord(word)
48+
# param_2 = obj.search(word)

0 commit comments

Comments
 (0)