Added 13 solutions
This commit is contained in:
@@ -10,27 +10,20 @@ public class Solution {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int N = scan.nextInt();
|
||||
scan.nextLine(); // gets rid of pesky newline
|
||||
HashMap<String, Integer> map = new HashMap<>();
|
||||
|
||||
/* Create and fill HashMap */
|
||||
HashMap<String, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < N; i++) {
|
||||
String str = scan.nextLine();
|
||||
if (map.containsKey(str)) {
|
||||
map.put(str, map.get(str) + 1);
|
||||
} else {
|
||||
map.put(str, 1);
|
||||
}
|
||||
map.merge(str, 1, Integer::sum);
|
||||
}
|
||||
|
||||
|
||||
/* Query HashMap */
|
||||
int Q = scan.nextInt();
|
||||
scan.nextLine(); // gets rid of pesky newline
|
||||
|
||||
for (int i = 0; i < Q; i++) {
|
||||
String str = scan.nextLine();
|
||||
if (map.containsKey(str)) {
|
||||
System.out.println(map.get(str));
|
||||
} else {
|
||||
System.out.println(0);
|
||||
}
|
||||
System.out.println(map.getOrDefault(str, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user