File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
leetcode.com 211. Design Add and Search Words Data Structure v2 Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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)
You can’t perform that action at this time.
0 commit comments