lc819.最常见的单词

 

题目链接

嗯自己写了一下,比较朴实(见1),应该有split函数可以直接按所有包含的标点分的,不过可能正在re里?然后用replace直接替换不想要的标点也会更快(见2)。这个算法的时间复杂度是O(N),空间复杂度是O(N)。

from collections import defaultdict

class Solution:
    def mostCommonWord1(self, paragraph: str, banned: List[str]) -> str:
        cnt = defaultdict(int)

        processed = ""
        for char in paragraph:
            if char not in "!?',;.":
                processed += char.lower()
            else:
                processed += " "
        words = processed.split()

        for w in words:
            cnt[w] += 1
        sorted_dic = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
        for item in sorted_dic:
            if item[0] not in banned:
                return item[0]

    def mostCommonWord2(self, paragraph: str, banned: List[str]) -> str:
        cnt = defaultdict(int)

        for c in paragraph:
            if c in "!?',;.":
                paragraph = paragraph.replace(c, " ")
        words = paragraph.lower().split()

        for w in words:
            cnt[w] += 1
        sorted_dic = sorted(cnt.items(), key=lambda x: x[1], reverse=True)
        for item in sorted_dic:
            if item[0] not in banned:
                return item[0]