Algorithm
Problem Name: Data Structures -
In this HackerRank in Data Structures -
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
stringList = ['ab',ab', abc']
queries = ['ab',abc',bc']
There are 2 instances of 'ab' , 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, result = [2,1,0]
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 n, the size of stringList[].
Each of the next n lines contains a string stringList[i].
The next line contains q, the size of queries[].
Each of the next q lines contains a string queries[i].
Constraints
1 <= n <= 1000
1 <= q <= 1000
1 <= stringList[i],queries[i] <= 20
Sample Input 1
aba
baba
aba
xzxb Array: queries
aba
xzxb
aba
baba
aba
xzxb
3
aba
xzxb
ab
Sample Output 1
1
0
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int N, Q;
char *N_array[1000], *Q_array[1000];
scanf("%d", &N);
for (int N_i = 0; N_i < N; N_i++) {
char s[21];
scanf("%s", s);
N_array[N_i] = malloc(21);
strcpy(N_array[N_i], s);
}
scanf("%d", &Q);
for (int Q_i = 0; Q_i < Q; Q_i++) {
int occurs = 0, result;
char s[21];
scanf("%s", s);
Q_array[Q_i] = malloc(21);
strcpy(Q_array[Q_i], s);
for (int N_i2 = 0; N_i2 < N; N_i2++) {
result = strcmp(Q_array[Q_i], N_array[N_i2]);
if (result == 0) occurs++;
}
printf("%d\n", occurs);
}
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
map < string,int> count;
int n;
cin>>n;
while(n--){
string s;
cin>>s;
count[s]++;
}
cin >> n;
while(n--){
string s;
cin >> s;
//cout << s << endl;
cout << count[s] << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
HashMap < String,Integer> hm = new HashMap<>();
for(int i = 0; i < N; ++i) {
String input = sc.next();
if(hm.get(input) == null) hm.put(input,1);
else {
int val = hm.get(input);
hm.put(input,++val);
}
}
int Q = sc.nextInt();
while(Q-->0) {
String query = sc.next();
if(hm.get(query) == null) System.out.println(0);
else System.out.println(hm.get(query));
}
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Javascript Programming
Code -
Javascript Programming
function processData(input) {
const split = input.split('\n');
const N = parseInt(split[0], 10);
var strings = split.slice(1, N + 1);
const Q = parseInt(split[N+1], 10);
const queries = split.slice(N+2);
const counts = [];
const countMap = new Map();
queries.forEach(query => {
if (countMap.has(query)) {
console.log(countMap.get(query))
} else {
const prevCount = strings.length;
strings = strings.filter(string => string !== query);
const nextCount = strings.length;
const count = prevCount - nextCount;
countMap.set(query, count);
console.log(count)
}
});
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Copy The Code &
Try With Live Editor
#5 Code Example with Python Programming
Code -
Python Programming
n = int(input())
hashmap = {}
for i in range(n):
string = input()
hashmap[string] = 1 if string not in hashmap else hashmap[string] + 1
q = int(input())
for j in range(q):
string = input()
print(0 if string not in hashmap else hashmap[string])
Copy The Code &
Try With Live Editor