Most Common Word


  • 0

    Click here to see the full article post


  • 0
    O

    My accepted solution in python

    from collections import Counter
    import re
    class Solution:
        def mostCommonWord(self, paragraph, banned):
            """
            :type paragraph: str
            :type banned: List[str]
            :rtype: str
            """
            paragraph = re.sub(r'[^\w\s]','',paragraph)        
            valids = [x.lower() for x in paragraph.split() if x.lower() not in banned]        
            return Counter(valids).most_common()[0][0]
    

  • 0
    Z

    We could use count.most_common() instead.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.