Created
June 3, 2017 08:42
-
-
Save rupalbarman/f7c6f8e187b534bf0ad6885dc0c4d43b to your computer and use it in GitHub Desktop.
trie using dict in python
This file contains hidden or 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
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