Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

819. Most Common Word

Posted on 2020-07-02 | Edited on 2021-01-22

LeetCode

link

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
String[] words = paragraph.toLowerCase().split("\\W+");
HashMap<String, Integer> map = new HashMap<>();
for(String word : words) map.put(word, map.getOrDefault(word, 0) + 1);
for(String word : banned) if(map.containsKey(word)) map.remove(word);
String res = null;
for(String word : map.keySet())
if(res == null || map.get(word) > map.get(res))
res = word;
return res;
}
}

<1…105106107…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%