-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidStrOcc.java
More file actions
50 lines (34 loc) · 1.03 KB
/
ValidStrOcc.java
File metadata and controls
50 lines (34 loc) · 1.03 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
import java.util.*;
class Solution {
public List<Integer> countWordOccurrences(
List<String> chunks,
List<String> queries) {
HashMap<String, Integer> freq = new HashMap<>();
// Combine all chunks
StringBuilder str = new StringBuilder();
for (String ch : chunks) {
str.append(ch).append(" ");
}
// Remove hyphens
String cleaned = str.toString().replace("-", "");
// Split into words
String[] words = cleaned.split("\\s+");
// Count frequencies
for (String word : words) {
if (!word.isEmpty()) {
freq.put(word, freq.getOrDefault(word, 0) + 1);
}
}
// Store answers
List<Integer> ans = new ArrayList<>();
for (String q : queries) {
ans.add(freq.getOrDefault(q, 0));
}
return ans;
}
}
public class ValidStrOcc {
public static void main(String[] args) {
Solution sol = new Solution();
}
}