819_Most Common Word
Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"Solution 1:
def mostCommonWord(paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
# replace all punctuations with space
for punc in string.punctuation:
# string.punctuation is a string containing all punctuations
paragraph = paragraph.replace(punc, " ")
words = paragraph.lower().split()
dict = {}
banned = set(banned)
for item in words:
if item not in banned:
dict[item] = dict.get(item, 0) + 1
return max(dict.keys(), key=(lambda k: dict[k]))Solution 2:
Last updated