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. Data Structures
  3. Arrays
  4. Sparse Arrays

Sparse Arrays

Problem
Submissions
Leaderboard
Discussions
Editorial
HackerRank Logo
|
  1. Prepare
  2. Data Structures
  3. Arrays
  4. Sparse Arrays
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.

Example


There are instances of '', of '' and of ''. For each query, add an element to the return array, .

Function Description

Complete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in stringList.

matchingStrings has the following parameters:

  • string stringList[n] - an array of strings to search
  • string queries[q] - an array of query strings

Returns

  • int[q]: an array of results for each query

Input Format

The first line contains and integer , the size of .
Each of the next lines contains a string .
The next line contains , the size of .
Each of the next lines contains a string .

Constraints



.

Sample Input 1

CopyDownload
G Array: stringList node1 aba baba aba xzxb G Array: queries node1 aba xzxb ab
4
aba
baba
aba
xzxb
3
aba
xzxb
ab

Sample Output 1

2
1
0

Explanation 1

Here, "aba" occurs twice, in the first and third string. The string "xzxb" occurs once in the fourth string, and "ab" does not occur at all.

Sample Input 2

CopyDownload
G Array: stringList node1 def de fgh G Array: queries node1 de lmn fgh
3
def
de
fgh
3
de
lmn
fgh

Sample Output 2

1
0
1

Sample Input 3

CopyDownload
G Array: stringList node1 abcde sdaklfj asdjf na basdn sdaklfj asdjf na asdjf na basdn sdaklfj asdjf G Array: queries node1 abcde sdaklfj asdjf na basdn
13
abcde
sdaklfj
asdjf
na
basdn
sdaklfj
asdjf
na
asdjf
na
basdn
sdaklfj
asdjf
5
abcde
sdaklfj
asdjf
na
basdn

Sample Output 3

1
3
4
3
2

  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • 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