-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostcommonword.java
More file actions
29 lines (26 loc) · 854 Bytes
/
Copy pathmostcommonword.java
File metadata and controls
29 lines (26 loc) · 854 Bytes
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
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
String punctuation = "!?',;. ";
String p = paragraph.toLowerCase();
p = p.replaceAll("[!?',;.]", "").trim();
String[] words = p.split(" ");
Map<String, Integer> counts = new HashMap();
for (String w: words) {
if (counts.containsKey(w)) {
counts.put(w, counts.get(w) + 1);
} else {
counts.put(w, 1);
}
}
String ans = "";
int max = 0;
List<String> b = Arrays.asList(banned);
for(String key: counts.keySet()) {
if (!b.contains(key) && counts.get(key)>max) {
ans = key;
max = counts.get(key);
}
}
return ans;
}
}