
https://leetcode.com/problems/most-common-word/description/
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
Map<String, Integer> wordCountMap = new HashMap<>();
for (String p : paragraph.split(" ")) {
String word = p.toLowerCase().replaceAll("[^a-zA-Z]", "");
if (!Arrays.asList(banned).contains(word)) {
int count = wordCountMap.getOrDefault(word, 0);
wordCountMap.put(word.toLowerCase(), count + 1);
}
}
return Collections.max(wordCountMap.entrySet(), Map.Entry.comparingByValue()).getKey();
}
}

class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
Map<String, Integer> wordCountMap = new HashMap<>();
for (String word : paragraph.toLowerCase().replaceAll("[^a-zA-Z]+", " ").split(" ")) {
if (!Arrays.asList(banned).contains(word)) {
int count = wordCountMap.getOrDefault(word, 0);
wordCountMap.put(word.toLowerCase(), count + 1);
}
}
return Collections.max(wordCountMap.entrySet(), Map.Entry.comparingByValue()).getKey();
}
}

Collections.max(wordCountMap.entrySet(), Map.Entry.comparingByValue()).getKey(); replaceAll("[^a-zA-Z]+", " ")^[a-zA-Z]는 알파벳이 아닌 글자를 한 글자 단위로 매칭시킨다. 즉, a, b는 ‘a’, ',’ , ‘ ’, ‘b’ 를 하나씩 매칭 시킨다.
⇒ a b (중간 공백 2칸)
^[a-zA-Z]+는 알파벳이 아닌 여러 글자를 한 번에 매칭 시킬 수 있다.a, b ⇒ ‘a’, ', ‘, ‘b’
⇒ a b (중간 공백 1칸)