-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMostCommonWord.java
51 lines (45 loc) · 1.63 KB
/
MostCommonWord.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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.*;
// https://leetcode.com/problems/most-common-word/
public class MostCommonWord {
private final String paragraph;
private final String[] banned;
public MostCommonWord(String paragraph, String[] banned) {
this.paragraph = paragraph;
this.banned = banned;
}
public String solution() {
Set<String> ban = new HashSet<>();
Collections.addAll(ban, banned);
String mostCommonWord = null;
StringBuilder word = new StringBuilder();
int counter = 0;
Map<String, Integer> wordsWithCount = new HashMap<>();
String inLowerCase = paragraph.toLowerCase();
for (int i = 0; i < inLowerCase.length(); i++) {
char c = inLowerCase.charAt(i);
if (Character.isLetter(c)) {
word.append(c);
} else if (word.length() > 0) {
String candidate = word.toString();
word.setLength(0);
if (!ban.contains(candidate)) {
int count = 1;
if (wordsWithCount.containsKey(candidate)) {
count = wordsWithCount.get(candidate);
count++;
}
wordsWithCount.put(candidate, count);
if (counter < count) {
counter = count;
mostCommonWord = candidate;
}
}
}
}
if (mostCommonWord == null) {
mostCommonWord = word.toString();
}
return mostCommonWord;
}
}