Created
July 31, 2018 09:09
-
-
Save luojiyin1987/c419dfe5d3def68928221d7c672f2500 to your computer and use it in GitHub Desktop.
Most Common Word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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