Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
Input: paragraph ="Bob hit a ball, the hit BALL flew far after it was hit.", banned =["hit"]Output: "ball"Explanation:
"hit" occurs 3 times, but it is a banned word."ball" occurs twice(and no other word does), so it is the most frequent non-banned word in the paragraph.Note that words in the paragraph are not case sensitive,that punctuation isignored(even if adjacent to words, such as "ball,"),and that "hit" isn't the answer even though it occurs more because it is banned.
publicclassSolution {
public String mostCommonWord(String paragraph, String[] banned) {
Set<String> bannedSet =new HashSet<>(Arrays.asList(banned));
Map<String, Integer> freq =new HashMap<>();
paragraph = paragraph.replaceAll("[^a-zA-Z]", " ").toLowerCase();
String[] words = paragraph.split("\\s+");
for (String word : words) {
if (!bannedSet.contains(word)) {
freq.put(word, freq.getOrDefault(word, 0) + 1);
}
}
String ans ="";
int maxCount = 0;
for (Map.Entry<String, Integer> entry : freq.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
ans = entry.getKey();
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defmostCommonWord(self, paragraph: str, banned: List[str]) -> str:
banned_set = set(banned)
words = re.findall(r'\w+', paragraph.lower())
freq = defaultdict(int)
for word in words:
if word notin banned_set:
freq[word] +=1 ans ='' max_count =0for word, count in freq.items():
if count > max_count:
max_count = count
ans = word
return ans
⏰ Time complexity: O(n + m), where n is the length of the paragraph and m is the number of banned words. This accounts for processing the paragraph, and for checking and counting word frequencies.
🧺 Space complexity: O(n + m) for storing the words, their frequencies, and the set of banned words.