当前位置: 首页 > 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.rkmt.cn/news/20583.html

相关文章:

  • 简谈误差与不确定度
  • 上下文丢失
  • 混淆矩阵
  • 提示词工程实践指南:从调参到对话的范式转变
  • 泛化能力
  • Python-weakref技术指南
  • 王爽《汇编语言》第四章 笔记
  • MySql安装中的问题
  • 10.14总结
  • 图形学中的变换
  • 使用DirectX绘制天空盒并实现破坏和放置方块
  • DirectX12初始化
  • 10月13日
  • Ubuntu22.04安装CH340/CH341驱动
  • STM32——UART
  • WebApi 交叉观察者- IntersectionObserver复盘
  • css: Bootstrap5 Accordions
  • AMPopTip - 优雅的iOS动画提示框库
  • 文件名中有空格比较烦人
  • 软工大三开学总结
  • 连接 USB 设备
  • SpringBoot-day1(快速上手SpringBoot,SpringBoot简介,SpringBoot基础配置,属性配置,yaml文件) - a
  • elk time
  • 详细介绍:【OpenHarmony】用户文件服务模块架构
  • 详细介绍:全新 CloudPilot AI:嵌入 Kubernetes 的 SRE Agent,降本与韧性双提升!
  • “环境变量”是什么, 为什么要配置环境变量 --初学者
  • Java 装饰器模式(Decorator) - krt
  • Python configparser 模块 - INI 文件读写利器
  • QT:获取文件信息之创建日期方法created()方法--废弃
  • 排列组合 容斥 总结