Leetcode819
思路
解答
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
String[] words = paragraph.replaceAll("\\pP"," ").toLowerCase().split("\\s+");
List<String> wordList = Arrays.asList(words);
HashMap<String, Integer> map = new HashMap<>();
wordList.stream().map(str -> str.trim()).forEach(str -> map.compute(str, (k, v) -> v == null ? 1 : v + 1));
for(String bw : banned){
if(map.containsKey(bw)){
map.remove(bw);
}
}
String ans = "";
int max = 0;
for(var kvp : map.entrySet()){
if(kvp.getValue() > max){
ans = kvp.getKey();
max = kvp.getValue();
}
}
return ans;
}
}
Vocabulary
xxxx [KK] : (注意事項)
xxxx [KK] : (注意事項)