819. Most Common Word

class Solution(object):
def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
res=''
cnt={res:0}
for word in paragraph.split():
word=word.lower().strip("!?',;.")
if word not in banned:
if word not in cnt:
cnt[word]=1
else:
cnt[word]+=1
if cnt[word]>cnt[res]:
res=word
return res


class Solution(object):
def mostCommonWord(self, paragraph, banned):
banset = set(banned)
count = collections.Counter(
word.strip("!?',;.") for word in paragraph.lower().split())

ans, best = '', 0
for word in count:
if count[word] > best and word not in banset:
ans, best = word, count[word]

return ans
posted @ 2018-09-13 03:03  ffeng0312  阅读(157)  评论(0)    收藏  举报