LeetCode 题解工作台

移除字母异位词后的结果数组

给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 由小写英文字符组成。 在一步操作中,需要选出任一下标 i ,从 words 中 删除 words[i] 。其中下标 i 需要同时满足下述两个条件: 0 words[i - 1] 和 words[i] 是 字母异位词 。 只要…

category

4

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·哈希·扫描

bolt

答案摘要

我们首先将 加入答案数组,然后从 开始遍历,如果 $\textit{words}[i - 1]$ 和 不是字母异位词,我们就将 加入答案数组。 问题转换为判断两个字符串是否是字母异位词,我们定义一个辅助函数 $\textit{check}(s, t)$ 来实现这个功能。如果 和 不是字母异位词,我们就返回 ,否则返回 。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的字符串数组 words ,其中 words[i] 由小写英文字符组成。

在一步操作中,需要选出任一下标 i ,从 words删除 words[i] 。其中下标 i 需要同时满足下述两个条件:

  1. 0 < i < words.length
  2. words[i - 1]words[i]字母异位词

只要可以选出满足条件的下标,就一直执行这个操作。

在执行所有操作后,返回 words 。可以证明,按任意顺序为每步操作选择下标都会得到相同的结果。

字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。例如,"dacb""abdc" 的一个字母异位词。

 

示例 1:

输入:words = ["abba","baba","bbaa","cd","cd"]
输出:["abba","cd"]
解释:
获取结果数组的方法之一是执行下述步骤:
- 由于 words[2] = "bbaa" 和 words[1] = "baba" 是字母异位词,选择下标 2 并删除 words[2] 。
  现在 words = ["abba","baba","cd","cd"] 。
- 由于 words[1] = "baba" 和 words[0] = "abba" 是字母异位词,选择下标 1 并删除 words[1] 。
  现在 words = ["abba","cd","cd"] 。
- 由于 words[2] = "cd" 和 words[1] = "cd" 是字母异位词,选择下标 2 并删除 words[2] 。
  现在 words = ["abba","cd"] 。
无法再执行任何操作,所以 ["abba","cd"] 是最终答案。

示例 2:

输入:words = ["a","b","c","d","e"]
输出:["a","b","c","d","e"]
解释:
words 中不存在互为字母异位词的两个相邻字符串,所以无需执行任何操作。

 

提示:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • words[i] 由小写英文字母组成
lightbulb

解题思路

方法一:模拟

我们首先将 words[0]\textit{words}[0] 加入答案数组,然后从 words[1]\textit{words}[1] 开始遍历,如果 words[i1]\textit{words}[i - 1]words[i]\textit{words}[i] 不是字母异位词,我们就将 words[i]\textit{words}[i] 加入答案数组。

问题转换为判断两个字符串是否是字母异位词,我们定义一个辅助函数 check(s,t)\textit{check}(s, t) 来实现这个功能。如果 sstt 不是字母异位词,我们就返回 true\text{true},否则返回 false\text{false}

在函数 check(s,t)\textit{check}(s, t) 中,我们首先判断 sstt 的长度是否相等,如果不相等,我们就返回 true\text{true}。否则,我们用一个长度为 2626 的数组 cnt\textit{cnt} 来统计 ss 中每个字符出现的次数,然后遍历 tt 中的每个字符,将 cnt[c]\textit{cnt}[c]11,如果 cnt[c]\textit{cnt}[c] 小于 00,我们就返回 true\text{true}。如果正常遍历完 tt 中的每个字符,说明 sstt 是字母异位词,我们返回 false\text{false}

时间复杂度 O(L)O(L),空间复杂度 O(Σ)O(|\Sigma|)。其中 LL 是数组 words\textit{words} 的长度,而 Σ\Sigma 是字符集,这里是小写英文字母,所以 Σ=26|\Sigma| = 26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def removeAnagrams(self, words: List[str]) -> List[str]:
        def check(s: str, t: str) -> bool:
            if len(s) != len(t):
                return True
            cnt = Counter(s)
            for c in t:
                cnt[c] -= 1
                if cnt[c] < 0:
                    return True
            return False

        return [words[0]] + [t for s, t in pairwise(words) if check(s, t)]
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Understanding of array scanning techniques and hash map usage.

  • question_mark

    Efficiency in detecting anagrams using frequency counts or sorting.

  • question_mark

    Ability to optimize for space and time complexity while solving.

warning

常见陷阱

外企场景
  • error

    Failing to recognize the need for an efficient anagram check (sorting or hash map).

  • error

    Not handling edge cases, like when no words are anagrams.

  • error

    Overcomplicating the solution with unnecessary operations or data structures.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing for a custom comparator instead of using frequency counts.

  • arrow_right_alt

    Modifying the input in place without using extra space.

  • arrow_right_alt

    Handling case sensitivity or extending the problem to a larger character set.

help

常见问题

外企场景

移除字母异位词后的结果数组题解:数组·哈希·扫描 | LeetCode #2273 简单