尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

[LeetCode] 2273. Find Resultant Array After Removing Anagrams

[LeetCode] 2273. Find Resultant Array After Removing Anagrams
📅 发布时间:2026/6/19 19:20:07

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);}
}

相关新闻

  • 简谈误差与不确定度
  • 上下文丢失
  • 混淆矩阵

最新新闻

  • Qwen3vl多模态后训练实战:LLamaFactory深度适配指南
  • 国产MLU算网+LLaMA-Factory:零代码微调百余大模型实战指南
  • 猫抓插件:3步搞定浏览器资源嗅探的终极指南
  • MPC866双核通信处理器架构解析与嵌入式网络设备开发实战
  • 存储型XSS漏洞实战解析:从DVWA靶场到安全防御
  • SRC漏洞挖掘实战:从信息搜集到逻辑漏洞的完整攻防指南

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号