LeetCode题解(0819):词频分析(Python)
发布日期:2021-06-29 19:54:23 浏览次数:2 分类:技术文章

本文共 689 字,大约阅读时间需要 2 分钟。

题目:(简单)

标签:字符串、哈希表

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N + B ) O(N+B) O(N+B) : N为句子长度、B为禁用列表长度 O ( N + B ) O(N+B) O(N+B) 40ms (92.22%)
Ans 2 (Python)
Ans 3 (Python)

解法一(哈希表):

def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:    hashmap = {
} for word in re.split("[ ,.?!]", paragraph.lower()): word = "".join(list(filter(str.isalpha, word))) if len(word) > 0: if word not in hashmap: hashmap[word] = 1 else: hashmap[word] += 1 times = 0 ans = "" for key, value in hashmap.items(): if value > times and key not in banned: ans = key times = value return ans

转载地址:https://dataartist.blog.csdn.net/article/details/107076195 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0821):到目标字符的最短距离(Python)
下一篇:LeetCode精讲(0812):在集合中取出三个点,组成最大面积三角形(Python)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月17日 11时08分17秒