-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathMostCommonWord.java
33 lines (30 loc) · 1.01 KB
/
MostCommonWord.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
package problems.easy;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
public class MostCommonWord {
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> set = new HashSet<>(Arrays.asList(banned));
String[] words = paragraph.split(" ");
Map<String, Integer> map = new HashMap<>();
for (String word : words) {
word = word.toLowerCase();
if (word.endsWith(",") || word.endsWith(";") || word.endsWith(".")
|| word.endsWith("!") || word.endsWith("?") || word.endsWith("'"))
word = word.substring(0, word.length() - 1);
if (set.contains(word))
continue;
map.put(word, map.getOrDefault(word, 0) + 1);
}
int max = 0;
String res = null;
for (String s : map.keySet()) {
if (map.get(s) > max) {
max = map.get(s);
res = s;
}
}
return res;
}
}