3295. 举报垃圾信息

WrRan / 2024-09-24 / 原文

题目链接 3295. 举报垃圾信息
思路 简单模拟即可
题解链接 哈希集合,简洁写法(Python/Java/C++/Go)
关键点
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)

代码实现:

class Solution:
    def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool:
        banned = set(bannedWords)
        cnt = 0
        for item in message:
            if item in banned:
                cnt += 1
            if cnt >= 2:
                return True
        return False