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
相关推荐
拓恒技术3 小时前
无人机巡检AI算法的工程化落地:从需求到交付的5个核心环节
人工智能·算法·无人机
晨曦中的暮雨3 小时前
限流算法——固定窗口、滑动窗口、漏桶、令牌桶
java·开发语言·算法
怕浪猫3 小时前
Claude Code vs Codex:Computer Use 实现原理深度拆解
算法·面试·github
文丑颜不良啊4 小时前
LeetCode876~900题目解析
java·leetcode
罗西的思考5 小时前
AI 时代的 “房谋杜断”:如何培养能力、品味与判断力
人工智能·算法·机器学习
淡海水5 小时前
07-02-源码-.NET Core GC分配路径
算法·unity·c#·.netcore·gc·1024程序员节
下一站春风里7 小时前
位运算完整学习笔记:按位与(&)+ 异或(^)
开发语言·c++·算法
DRobot7 小时前
【机械制图与CAD实战(二十五)】螺纹的表示方法
算法·机械制图
用户老李7 小时前
卡尔曼滤波自学教程 10:无迹卡尔曼滤波(UKF)
算法
救救孩子把7 小时前
AI面试八股文-23_23-Agent可观测性与生产监控面试题
人工智能·面试·职场和发展·agent·langsmith·langfuase