819. Most Common Word
题目
解题思路
先将paragraph转化成小写,然后按非字母分隔,得到一连串字符。需要使用re表达式,然后使用Counter计数各个单词的出现次数,跟banned中比对,得出正确的结果。
我的代码(效率较快)
import re
from collections import Counter
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words=re.split('\W+',paragraph.lower())
cnt=Counter()
for word in words:
cnt[word]+=1
most=cnt.most_common()
for word in most:
if word[0] not in banned:
return word[0]