Skip to content

Commit

Permalink
Trie
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 11, 2021
1 parent eda442c commit 900c860
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Coding Test/trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import re
import collections
import itertools
import heapq
import bisect
import sys
import functools

class TrieNode:
def __init__(self):
self.word = False
self.children = collections.defaultdict(TrieNode)

class Trie:
def __init__(self):
self.root = TrieNode()

def insert(self, word: str) -> None:
node = self.root
for char in word:
node = node.children[char]
node.word = True

def search(self, word: str) -> bool:
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]

return node.word

def startsWith(self, prefix: str) -> bool:
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]


return True

0 comments on commit 900c860

Please sign in to comment.