Last active
July 30, 2020 13:11
-
-
Save jsrimr/2cf07262f6f000ce60984b492567aacf to your computer and use it in GitHub Desktop.
트라이구현
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Trie: | |
def __init__(self): | |
self.head = {} | |
def add(self, word): | |
cur = self.head | |
for ch in word: | |
if ch not in cur: | |
cur[ch] = {} | |
cur = cur[ch] | |
# *가 있을 경우, 그 단어가 자료구조에 저장되어 있음을 의미 | |
# *가 없을 경우, 그 단어는 자료구조에 저장되어 있지 않음, 단지 더 긴 단어의 부분으로만 존재함 | |
cur['*'] = True | |
def search(self, word): | |
cur = self.head | |
for ch in word: | |
if ch not in cur: | |
return False | |
cur = cur[ch] | |
if '*' in cur: | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment