Find the Most Frequent Non-Banned Word in a Paragraph

Quick Overview

Given a paragraph and a list of banned words, find the most frequent word that is not banned, ignoring case and punctuation. It tests string tokenizing, hash map counting, set lookups and applying a clear tie-breaking rule.

Find the Most Frequent Non-Banned Word in a Paragraph

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a `paragraph` string and an array of banned words, find the most frequently occurring word that is not in the banned list. The comparison should be case-insensitive, and the answer should be returned in lowercase. Words in the paragraph may contain punctuation marks which should be ignored. ### Function Signature ```python def most_common_word(paragraph: str, banned: list[str]) -> str: ``` ### Rules - A word is a maximal run of English letters. Spaces and punctuation symbols only separate words, so `b,b,b` holds three words and `ball,` is the word `ball`. - Words are compared after lowercasing, so `Ball` and `BALL` are both `ball`. - At least one word in `paragraph` is not banned. - If several non-banned words share the highest count, return the one whose first occurrence comes earliest in the paragraph. ### Constraints - `1 <= paragraph.length <= 1000` - `paragraph` consists of English letters, space `' '`, or punctuation symbols - `0 <= banned.length <= 100` - `1 <= banned[i].length <= 10` - `banned[i]` consists of only lowercase English letters ### Examples ```text Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"] Output: "ball" ``` The word `hit` appears three times but is banned. After lowercasing, `ball` appears twice and every other word once. ```text Input: paragraph = "a, a, a, a, b,b,b,c, c", banned = ["a"] Output: "b" ``` The commas separate words, so the counts are `a` 4, `b` 3 and `c` 2. With `a` banned, `b` has the highest count. ```text Input: paragraph = "Dog cat, CAT dog. bird", banned = [] Output: "dog" ``` Both `dog` and `cat` appear twice. The tie goes to `dog` because it occurs first in the paragraph.

Overview: Given a paragraph and a list of banned words, find the most frequent word that is not banned, ignoring case and punctuation. It tests string tokenizing, hash map counting, set lookups and applying a clear tie-breaking rule.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a paragraph string and an array of banned words, find the most frequently occurring word that is not in the banned list. The comparison should be case-insensitive, and the answer should be returned in lowercase. Words in the paragraph may contain punctuation marks which should be ignored.

Function Signature

def most_common_word(paragraph: str, banned: list[str]) -> str:

Rules

  • A word is a maximal run of English letters. Spaces and punctuation symbols only separate words, so b,b,b holds three words and ball, is the word ball .
  • Words are compared after lowercasing, so Ball and BALL are both ball .
  • At least one word in paragraph is not banned.
  • If several non-banned words share the highest count, return the one whose first occurrence comes earliest in the paragraph.

Constraints

  • 1 <= paragraph.length <= 1000
  • paragraph consists of English letters, space ' ' , or punctuation symbols
  • 0 <= banned.length <= 100
  • 1 <= banned[i].length <= 10
  • banned[i] consists of only lowercase English letters

Examples

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

The word hit appears three times but is banned. After lowercasing, ball appears twice and every other word once.

Input: paragraph = "a, a, a, a, b,b,b,c, c", banned = ["a"]
Output: "b"

The commas separate words, so the counts are a 4, b 3 and c 2. With a banned, b has the highest count.

Input: paragraph = "Dog cat, CAT dog. bird", banned = []
Output: "dog"

Both dog and cat appear twice. The tie goes to dog because it occurs first in the paragraph.