Skip to content

Instantly share code, notes, and snippets.

@jsrimr
Last active July 30, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsrimr/2cf07262f6f000ce60984b492567aacf to your computer and use it in GitHub Desktop.
Save jsrimr/2cf07262f6f000ce60984b492567aacf to your computer and use it in GitHub Desktop.
트라이구현
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