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.