511/670

511. Most Common Word

Easy

You are given a block of text paragraph and a list of lowercase words banned. Words in the paragraph are separated by spaces and/or punctuation (! ? ' , ; .), and word comparison is case-insensitiveBall, BALL, and ball are the same word.

Your function receives paragraph and banned and must return the word that occurs most often while not appearing on the banned list, written in lowercase.

The input guarantees that at least one non-banned word exists and that the winner is unique, so the answer is always well-defined.

Example 1:

Input: paragraph = "The quick fox jumped; the lazy dog barked. THE fox ran.", banned = ["the"]

Output: "fox"

Explanation: Lowercased, "the" appears 3 times but is banned. "fox" appears twice — more than any other allowed word — so it wins.

Example 2:

Input: paragraph = "a.", banned = []

Output: "a"

Explanation: The only word is "a" (the period is a separator, not part of the word), and nothing is banned.

Constraints:

  • 1 ≤ paragraph.length ≤ 1000
  • paragraph consists of English letters, spaces, and the punctuation marks ! ? ' , ; .
  • 0 ≤ banned.length ≤ 100
  • 1 ≤ banned[i].length ≤ 10; every banned[i] is lowercase letters only
  • At least one word is not banned, and the most frequent non-banned word is unique

Hints:

Split on anything that is not a letter — punctuation can be glued straight onto a word ("ball," or "hit!"), so splitting on spaces alone under-counts. Lowercase characters as you collect them.

Load the banned words into a hash set for O(1) rejection, count the remaining words in a hash map, and keep the running maximum while you count — no second pass needed.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: paragraph = "The quick fox jumped; the lazy dog barked. THE fox ran.", banned = ["the"]

Expected output: "fox"