File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ ์ด๋ฒ ๋ฌธ์ ๋ Trie ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ๋ฌธ์ ์์ต๋๋ค.
3+ ์ด๋ฅผ ํ์
ํ์ง ๋ชปํด์ ์ ์ฒด ํ์ํ๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์๋ค ๊ฒฐ๊ตญ ๋ ๋์์ ๋ฐ์ ์ดํดํ๋ ๊ฒ์ ๋ชฉํ๋ฅผ ๋์ต๋๋ค.
4+ '''
5+ class WordDictionary :
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+ return any (dfs (node [k ], idx + 1 ) for k in node if k != "$" )
26+ return False
27+ return dfs (self .root , 0 )
You canโt perform that action at this time.
0 commit comments