Most Common Word

Given a string paragraph and a string array of banned words, return the most frequent word in the paragraph that is not banned. It is guaranteed that at least one word is not banned and that the answer is unique. Words are case-insensitive, punctuation separates words, and the answer must be returned in lowercase.

30:00

Most Common Word
easy
Free written solution
Topics
Companies

Given a string paragraph and a string array of banned words, return the most frequent word in the paragraph that is not banned. It is guaranteed that at least one word is not banned and that the answer is unique. Words are case-insensitive, punctuation separates words, and the answer must be returned in lowercase.

Example 1:
Input: {"paragraph":"Bob hit a ball, the hit BALL flew far after it was hit.","banned":["hit"]}
Output: ball
Constraints:
  • 1≤paragraph.length≤10001 \leq \text{paragraph.length} \leq 1000

  • paragraph consists of English letters, space, and the symbols !?',;.

  • 0≤banned.length≤1000 \leq \text{banned.length} \leq 100

  • 1≤banned[i].length≤101 \leq \text{banned}[i]\text{.length} \leq 10

  • banned[i] consists of only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"paragraph":"Bob hit a ball, the hit BALL flew far after it was hit.","banned":["hit"]}

Put the 1 banned word into a set so each check is O(1).

paragraph

Bob hit a ball, the hit BALL flew far after it was hit.

banned set (skipped)

hit ×0

count map (tally)

no words counted yet

leader: none yet

Variables
VariableValue
best""
best_count0
DepthFunction Call
Stack empty
0/28