Leetcode 2273. Find Resultant Array After Removing Anagrams

Problem

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

In one operation, select any index i such that 0 < i < words.length and wordsi - 1 and wordsi are anagrams, and delete wordsi 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".

Algorithm

The number of occurrences of each letter forms a tuple for comparison.

Code

python3 复制代码
class Solution:
    def removeAnagrams(self, words: List[str]) -> List[str]:
        def letters(word: str):
            cnt = [0] * 26
            for ch in word:
                cnt[ord(ch) - ord('a')] += 1
            return tuple(cnt)

        pre, ans = None, []
        for word in words:
            s = letters(word)
            if s != pre:
                ans.append(word)
                pre = s
        
        return ans
相关推荐
叩码以求索35 分钟前
浅谈:前序遍历反转法求解N叉树的后序遍历
算法
官乐2 小时前
AI面试指南(多agent开发流程)
人工智能·面试·职场和发展
北风toto2 小时前
中缀、前缀、后缀表达式
算法·软件设计师
程序员三藏2 小时前
自动化测试用例编写详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
听取WA声一片(无恶意)2 小时前
CSP-J/CSP-S 深度优先搜索(DFS)完全讲义
c++·算法·深度优先
跟我一起学测试呀2 小时前
软件测试面试总结,备战金九银十
面试·职场和发展
码流怪侠2 小时前
2026年8月GitHub热榜深度拆解:Agent Skills席卷开源圈,一个“技能包“收割5万星
算法·程序员·github
hold?fish:palm2 小时前
30 两两交换链表中的节点
数据结构·算法·链表
Nil2083 小时前
leetcode 98验证二叉搜索树
算法·leetcode·职场和发展
不正经学生3 小时前
C语言结构体:自定义数据类型,让变量打包出行
java·c语言·开发语言·数据结构·算法