#819
Most Common Word
pupil · 535 · lc easy +28 · verified · 45% accepted · 1,846 likes · top 28%
Description
You are given a string paragraph and a list of banned words banned. Return the most frequently occurring word in paragraph that does not appear in banned. The answer is guaranteed to be unique, and there is always at least one non-banned word.
The comparison is case-insensitive; return the result in lowercase.
Words cannot include punctuation characters.
Example 1:
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 is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.
Example 2:
Input: paragraph = "a.", banned = []
Output: "a"
Code
1
2
3