Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created June 3, 2017 08:42
Show Gist options
  • Save rupalbarman/f7c6f8e187b534bf0ad6885dc0c4d43b to your computer and use it in GitHub Desktop.
Save rupalbarman/f7c6f8e187b534bf0ad6885dc0c4d43b to your computer and use it in GitHub Desktop.
trie using dict in python
N = int(input())
d = {}
for _ in range(N):
cmd = input().strip().split()
if cmd[0] == 'add':
s = cmd[1]
for i in range(len(s)):
prefix = s[:i+1]
d[prefix] = d.get(prefix, 0) + 1
elif cmd[0] == 'find':
s = cmd[1]
print(str(d.get(s, 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment