/
Leetcode-819-Most-Common-Word.java
55 lines (44 loc) · 1.21 KB
/
Leetcode-819-Most-Common-Word.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.HashMap;
import java.util.Map;
/**
* Leetcode 819. Most Common Word
*
* @author EunHyeJung
* @email ehye826@gmail.com
* @date 2018.07.03
* @link https://leetcode.com/problems/most-common-word/description/
*/
public class Solution {
public String mostCommonWord(String paragraph, String[] bannedWords) {
String[] splits = paragraph.toLowerCase().split(" ");
Map<String, Integer> wordCounter = new HashMap<>();
boolean isBannedWord = false;
for (String split : splits) {
String word = split.replaceAll("[^A-Za-z]", "");
isBannedWord = false;
for (String bannedWord : bannedWords) {
if (word.equals(bannedWord)) {
isBannedWord = true;
break;
}
}
if (!isBannedWord) {
wordCounter.put(word, wordCounter.getOrDefault(word, 0) + 1);
}
}
int maxCnt = Integer.MIN_VALUE;
String mostCommonWord = null;
for (String word : wordCounter.keySet()) {
if (maxCnt < wordCounter.get(word)) {
maxCnt = wordCounter.get(word);
mostCommonWord = word;
}
}
return mostCommonWord;
}
public static void main(String[] args) {
String[] bannedWords = {};
String res = new Solution().mostCommonWord("a.", bannedWords);
System.out.println(res);
}
}