当前位置: 首页 > news >正文

[LeetCode] 2273. Find Resultant Array After Removing Anagrams

You are given a 0-indexed string array words, where words[i] consists of lowercase English letters.

In one operation, select any index i such that 0 < i < words.length and words[i - 1] and words[i] are anagrams, and delete words[i] from words. Keep performing this operation as long as you can select an index that satisfies the conditions.

Return words after performing all operations. It can be shown that selecting the indices for each operation in any arbitrary order will lead to the same result.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, "dacb" is an anagram of "abdc".

Example 1:
Input: words = ["abba","baba","bbaa","cd","cd"]
Output: ["abba","cd"]
Explanation:
One of the ways we can obtain the resultant array is by using the following operations:

  • Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
    Now words = ["abba","baba","cd","cd"].
  • Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
    Now words = ["abba","cd","cd"].
  • Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
    Now words = ["abba","cd"].
    We can no longer perform any operations, so ["abba","cd"] is the final answer.

Example 2:
Input: words = ["a","b","c","d","e"]
Output: ["a","b","c","d","e"]
Explanation:
No two adjacent strings in words are anagrams of each other, so no operations are performed.

Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 10
words[i] consists of lowercase English letters.

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

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

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

0 < i < words.length
words[i - 1] 和 words[i] 是 字母异位词 。
只要可以选出满足条件的下标,就一直执行这个操作。

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

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

思路

从左到右遍历 words,维护上一次「保留下来的单词」,记为 prev。当前单词 cur 与 prev 相同则为相邻变位词,跳过;否则加入答案并更新 prev。

复杂度

时间O(n)
空间O(k * klogk) - k是words的长度,klogk是排序的复杂度

代码

Java实现

class Solution {public List<String> removeAnagrams(String[] words) {List<String> res = new ArrayList<>();String prev = ""; // 上一个保留单词的排序签名for (String word : words) {String sorted = helper(word);if (!sorted.equals(prev)) { // 如果不是变位词res.add(word); // 加入原单词prev = sorted; // 更新签名}}return res;}private String helper(String word) {char[] letters = word.toCharArray();Arrays.sort(letters);return new String(letters);}
}
http://www.jsqmd.com/news/13205/

相关文章:

  • 251013
  • 简谈误差与不确定度
  • 可怕!我的Nodejs系统因为日志打印了Error 对象就崩溃了 Node.js System Crashed Because of Logging an Error
  • 实践
  • 数据结构字符串和图
  • 字典dict
  • 结婚证识别技术:融合计算机视觉、深度学习与自然语言处理的综合性AI能力的体现
  • 上下文丢失
  • 数据结构序列
  • 上下文学习(In-context Learning, ICL)
  • 混淆矩阵
  • 提示词工程实践指南:从调参到对话的范式转变
  • 泛化能力
  • JVM引入
  • shiro 架构
  • test9 - post
  • Python-weakref技术指南
  • 从众多知识汲取一星半点也能受益匪浅【day11(2025.10.13)】
  • 王爽《汇编语言》第四章 笔记
  • 10.13总结
  • MySql安装中的问题
  • 10.14总结
  • 题解:AT_agc050_b [AGC050B] Three Coins
  • go:generate 指令
  • 光栅化
  • 图形学中的变换
  • Unity URP 体积云
  • 使用DirectX绘制天空盒并实现破坏和放置方块
  • 编写DX12遇到的坑
  • 编写DX12时使用的辅助类