Most Common Word

Problem Id: 819 Difficulty: Easy


Intuition

Solution


class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        # Remove punctuation symbols
        s = ''
        for c in paragraph:
            if c not in "!?',;.":
                s += c
            else:
                s += ' '
        paragraph = s.lower()

        banned = set(banned + [''])
        words = [w for w in paragraph.split(' ') if w not in banned]
        return collections.Counter(words).most_common(1)[0][0]