Skip to content

Instantly share code, notes, and snippets.

@arishal
Last active July 12, 2018 21:05
Show Gist options
  • Select an option

  • Save arishal/a2d4b9acba430335e0068f028f3393c4 to your computer and use it in GitHub Desktop.

Select an option

Save arishal/a2d4b9acba430335e0068f028f3393c4 to your computer and use it in GitHub Desktop.
# https://www.hackerrank.com/challenges/contacts/problem
# We're going to make our own Contacts application! The application must perform two types of operations:
# 1. add name, where name is a string denoting a contact name. This must store name as a new contact in
# the application.
# 2. find partial, where partial is a string denoting a partial name to search the application for. It
# must count the number of contacts starting with partial and print the count on a new line.
# Given n sequential add and find operations, perform each operation in order.
# INPUT FORMAT:
# The first line contains a single integer, n, denoting the number of operations to perform.
# Each line i of the n subsequent lines contains an operation in one of the two forms defined above.
from collections import defaultdict
contacts = []
partials = [defaultdict(lambda: 0) for _ in range(22)]
for _ in range(int(input())):
q = input().split()
if q[0] == 'add':
for i in range(1, len(q[1]) + 1):
partials[i][q[1][:i]] += 1
else:
print(partials[len(q[1])][q[1]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment