Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 31, 2018 09:09
Show Gist options
  • Save luojiyin1987/c419dfe5d3def68928221d7c672f2500 to your computer and use it in GitHub Desktop.
Save luojiyin1987/c419dfe5d3def68928221d7c672f2500 to your computer and use it in GitHub Desktop.
Most Common Word
class Solution:
def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
p = re.compile(r"[!?',;.]")
paraStrs = p.sub('', paragraph.lower()).split(' ')
words = [ word for word in paraStrs if word not in banned]
count = collections.Counter(words)
return count.most_common(1)[0][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment