Skip to content

Instantly share code, notes, and snippets.

@xplorld
Last active August 28, 2019 03:03
Show Gist options
  • Save xplorld/7844896fd51dc4cf57766d936ca45683 to your computer and use it in GitHub Desktop.
Save xplorld/7844896fd51dc4cf57766d936ca45683 to your computer and use it in GitHub Desktop.
leetcode-safe trie
import collections
class Trie:
def __init__(self):
self.children = collections.defaultdict(Trie)
self.is_leaf = False
def add(self, string):
if string == '':
self.is_leaf = True
else:
head = string[0]
tail = string[1:]
self.children[head].add(tail)
def contains(self, string):
if string == '':
return self.is_leaf
else:
head = string[0]
tail = string[1:]
return self.children[head].contains(tail)
@at15
Copy link

at15 commented Aug 28, 2019

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment