-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path819.most-common-word.java
More file actions
37 lines (31 loc) · 954 Bytes
/
819.most-common-word.java
File metadata and controls
37 lines (31 loc) · 954 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
30
31
32
33
34
35
36
/*
* @lc app=leetcode id=819 lang=java
*
* [819] Most Common Word
*/
// @lc code=start
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
String paraLowerCase = paragraph.replaceAll("[,\\?\\!\\'\\;\\.]", " ").toLowerCase();
String[] words = paraLowerCase.split("\\s+");
Set<String> set = new HashSet<>();
for (String word : banned) {
set.add(word);
}
Map<String, Integer> map = new HashMap();
for (String word : words) {
if (!set.contains(word)) {
System.out.println(word);
map.put(word, map.getOrDefault(word, 0) + 1);
}
}
int max = 0;
String maxKey = "";
for (String key : map.keySet()) {
max = Math.max(max, map.get(key));
maxKey = max == map.get(key) ? key : maxKey;
}
return maxKey;
}
}
// @lc code=end