-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMostCommonWord.java
More file actions
22 lines (20 loc) · 915 Bytes
/
MostCommonWord.java
File metadata and controls
22 lines (20 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.dbc;
import java.util.*;
public class MostCommonWord {
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> hit = new HashSet<>();
Map<String, Integer> counter = new HashMap<>();
for (String temp : banned) hit.add(temp);
int left = 0, right = 0;
String res = "";
while (right < paragraph.length()) {
while (right < paragraph.length() && Character.isLetter(paragraph.charAt(right))) right++;
String letter = paragraph.substring(left, right).toLowerCase();
counter.put(letter, counter.getOrDefault(letter, 0) + 1);
if (counter.get(letter) > counter.getOrDefault(res, 0) && !hit.contains(letter)) res = letter;
while (right < paragraph.length() && !Character.isLetter(paragraph.charAt(right))) right++;
left = right;
}
return res;
}
}