Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
titlemost common word
linenumberstrue
import re


class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        tokens = re.split("[!?',;. ]", paragraph)
        words = list(map(lambda x: re.sub(r"[!?',;.]", "", x), tokens))
        words = list(map(lambda x: x.lower(), words))
        words = list(filter(lambda x: x != '', words))
        dic = {}
        for word in words:
            dic.setdefault(word, 0)
            dic[word] += 1

        for b in banned:
            dic.pop(b, None)

        max = 0
        for d in dic:
            if dic[d] > max:
                max = dic[d]
                ans = d
        return ans