We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 13
  5. Contacts

Contacts

Problem
Submissions
Leaderboard
Discussions
Editorial
HackerRank Logo
|
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 13
  5. Contacts
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

We're going to make our own Contacts application! The application must perform two types of operations:

  1. add name, where is a string denoting a contact name. This must store as a new contact in the application.
  2. find partial, where is a string denoting a partial name to search the application for. It must count the number of contacts starting with and print the count on a new line.

Given sequential add and find operations, perform each operation in order.

Example
Operations are requested as follows:

add ed
add eddie
add edward
find ed
add edwina
find edw
find a

Three operations include the names 'ed', 'eddie', and 'edward'. Next, matches all names. Note that it matches and counts the entire name 'ed'. Next, add 'edwina' and then find 'edw'. names match: 'edward' and 'edwina'. In the final operation, there are names that start with 'a'. Return the array .

Function Description

Complete the contacts function below.

contacts has the following parameters:

  • string queries[n]: the operations to perform

Returns

  • int[]: the results of each find operation

Input Format

The first line contains a single integer, , the number of operations to perform (the size of ).
Each of the following lines contains a string, .

Constraints

  • and contain lowercase English letters only.
  • The input does not have any duplicate for the operation.

Sample Input

STDIN           Function
-----           --------
4               queries[] size n = 4
add hack        queries = ['add hack', 'add hackerrank', 'find hac', 'find hak']
add hackerrank
find hac
find hak

Sample Output

2
0

Explanation

  1. Add a contact named hack.
  2. Add a contact named hackerrank.
  3. Find the number of contact names beginning with hac. Both name start with hac, add to the return array.
  4. Find the number of contact names beginning with hak. neither name starts with hak, add to the return array.
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website