Last active
July 12, 2018 21:05
-
-
Save arishal/a2d4b9acba430335e0068f028f3393c4 to your computer and use it in GitHub Desktop.
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
| # 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