forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_819.java
29 lines (27 loc) · 992 Bytes
/
_819.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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class _819 {
public static class Solution1 {
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> bannedSet = new HashSet(Arrays.asList(banned));
String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
Map<String, Integer> map = new HashMap<>();
Arrays.stream(words)
.filter(word -> !bannedSet.contains(word))
.forEach(word -> map.put(word, map.getOrDefault(word, 0) + 1));
String result = "";
int freq = 0;
for (String key : map.keySet()) {
if (map.get(key) > freq) {
result = key;
freq = map.get(key);
}
}
return result;
}
}
}