Leetcode 2273. Find Resultant Array After Removing Anagrams

Problem

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".

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
相关推荐
curry____3038 分钟前
基本算法(2025.11.21)
c++·算法
WWZZ20251 小时前
快速上手大模型:深度学习5(实践:过、欠拟合)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
司铭鸿1 小时前
图论中的协同寻径:如何找到最小带权子图实现双源共达?
linux·前端·数据结构·数据库·算法·图论
小年糕是糕手3 小时前
【C++】C++入门 -- 输入&输出、缺省参数
c语言·开发语言·数据结构·c++·算法·leetcode·排序算法
川Princess3 小时前
【面试经验】百度Agent架构研发工程师一面
面试·职场和发展·架构·agent
情怀姑娘3 小时前
面试题---------------场景+算法
java·算法·mybatis
chbmvdd3 小时前
week5题解
数据结构·c++·算法
用户12039112947263 小时前
面试官最爱问的字符串反转:7种JavaScript实现方法详解
算法·面试
vir024 小时前
小齐的技能团队(dp)
数据结构·c++·算法·图论
Star在努力4 小时前
C语言复习八(2025.11.18)
c语言·算法·排序算法