819 Most Common Word

python;

class Solution:
    def mostCommonWord(self, paragraph, banned):
        """
        :type paragraph: str
        :type banned: List[str]
        :rtype: str
        """
        res = ('', -1)
        dic = dict()
        symbols = ["!", "?", "'", ",", ";", "."]
        for symbol in symbols:
            paragraph = paragraph.replace(symbol, " ")

        for i in paragraph.split(" "):
            i = i.lower().strip()
            if i and i not in banned:
                dic[i] = dic.get(i, 0) + 1
                if dic[i] > res[1]:
                    res = (i, dic[i])
        print (dic)
        return res[0]

猜你喜欢

转载自blog.csdn.net/MRxjh/article/details/83308861