forked from builder-of-web3/HackR_Java_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse Arrays
More file actions
71 lines (60 loc) · 1.77 KB
/
Sparse Arrays
File metadata and controls
71 lines (60 loc) · 1.77 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
There are NN strings. Each string's length is no more than 2020 characters. There are also QQ queries. For each query, you are given a string, and you need to find out how many times this string occurred previously.
Input Format
The first line contains NN, the number of strings.
The next NN lines each contain a string.
The N+2N+2nd line contains QQ, the number of queries.
The following QQ lines each contain a query string.
Constraints
1≤N≤10001≤N≤1000
1≤Q≤10001≤Q≤1000
1≤1≤ lengthlength ofof anyany string≤20string≤20
Sample Input
4
aba
baba
aba
xzxb
3
aba
xzxb
ab
Sample Output
2
1
0
Explanation
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.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//System.out.println(n);
String[] str1 = new String[n];
str1[0] = sc.nextLine();
for(int i=0;i<n;i++){
str1[i] = sc.nextLine();
//System.out.println(str1[i]);
}
int n2 = sc.nextInt();
String[] str2 = new String[n2];
str2[0] = sc.nextLine();
for(int i=0;i<n2;i++){
str2[i] = sc.nextLine();
}
for(int i=0;i<n2;i++){
int count = 0;
for(int j=0;j<n;j++){
if(str2[i].equals(str1[j])){
count++;
}
}
System.out.println(count);
}
}
}