-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseArrays.java
37 lines (33 loc) · 1.09 KB
/
SparseArrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
There are N strings. Each string's length is no more than
20 characters. There are also Q queries. For each query, you
are given a string, and you need to find out how many times
this string occurred previously.
Link: https://www.hackerrank.com/challenges/sparse-arrays
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
// Not an arrays solution; used hash map for efficiency
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
Scanner s = new Scanner(System.in);
int count = s.nextInt();
for (int i = 0; i < count; i++) {
String input = s.next();
if (map.containsKey(input)) {
map.put(input, map.get(input)+1);
}else {
map.put(input, 1);
}
}
count = s.nextInt();
for (int i = 0; i < count; i++) {
String query = s.next();
System.out.println( map.containsKey(query) ? map.get(query) : 0 );
}
}
}