LeetCode 题解工作台

举报垃圾信息

给你一个字符串数组 message 和一个字符串数组 bannedWords 。 如果数组中 至少 存在两个单词与 bannedWords 中的任一单词 完全相同 ,则该数组被视为 垃圾信息 。 如果数组 message 是垃圾信息,则返回 true ;否则返回 false 。 示例 1: 输入: …

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们用一个哈希表 存储 中的所有单词,然后遍历 中的每个单词,如果单词在哈希表 中出现,我们就将计数器 加一,如果 大于等于 ,我们就返回 ,否则返回 。 时间复杂度 $O((n + m) \times |w|)$,空间复杂度 $O(m \times |w|)$。其中 是数组 的长度,而 和 分别是数组 的长度和数组中单词的最大长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串数组 message 和一个字符串数组 bannedWords

如果数组中 至少 存在两个单词与 bannedWords 中的任一单词 完全相同,则该数组被视为 垃圾信息

如果数组 message 是垃圾信息,则返回 true;否则返回 false

 

示例 1:

输入: message = ["hello","world","leetcode"], bannedWords = ["world","hello"]

输出: true

解释:

数组 message 中的 "hello""world" 都出现在数组 bannedWords 中。

示例 2:

输入: message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]

输出: false

解释:

数组 message 中只有一个单词("programming")出现在数组 bannedWords 中。

 

提示:

  • 1 <= message.length, bannedWords.length <= 105
  • 1 <= message[i].length, bannedWords[i].length <= 15
  • message[i]bannedWords[i] 都只由小写英文字母组成。
lightbulb

解题思路

方法一:哈希表

我们用一个哈希表 ss 存储 bannedWords\textit{bannedWords} 中的所有单词,然后遍历 message\textit{message} 中的每个单词,如果单词在哈希表 ss 中出现,我们就将计数器 cntcnt 加一,如果 cntcnt 大于等于 22,我们就返回 true\text{true},否则返回 false\text{false}

时间复杂度 O((n+m)×w)O((n + m) \times |w|),空间复杂度 O(m×w)O(m \times |w|)。其中 nn 是数组 message\textit{message} 的长度,而 mmw|w| 分别是数组 bannedWords\textit{bannedWords} 的长度和数组中单词的最大长度。

1
2
3
4
5
class Solution:
    def reportSpam(self, message: List[str], bannedWords: List[str]) -> bool:
        s = set(bannedWords)
        return sum(w in s for w in message) >= 2
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's ability to implement efficient searching using a hash set.

  • question_mark

    Evaluate the candidate's approach to optimizing for early exits.

  • question_mark

    Test the candidate's understanding of time complexity, particularly with large arrays.

warning

常见陷阱

外企场景
  • error

    Incorrect handling of case sensitivity: The problem specifies lowercase letters only, but candidates might mistakenly use case-insensitive checks.

  • error

    Not using a hash set: A naive approach without hash sets could lead to slow performance for large inputs.

  • error

    Failing to optimize for early exit: Checking the entire array even after finding two banned words is inefficient.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Instead of returning true or false, return the list of banned words found in the message.

  • arrow_right_alt

    Handle edge cases where the `message` or `bannedWords` arrays are empty.

  • arrow_right_alt

    Optimize for constant space complexity by scanning the array and checking banned words inline without using a hash set.

help

常见问题

外企场景

举报垃圾信息题解:数组·哈希·扫描 | LeetCode #3295 中等