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
相关推荐
通信小呆呆11 小时前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
benben04411 小时前
强化学习之DQN算法族(基于gymnasium开发)
算法
何以解忧,唯有..13 小时前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
想吃火锅100513 小时前
【leetcode】88.合并两个有序数组js
算法
生成论实验室14 小时前
机器人:一个自主运动的系统
人工智能·算法·语言模型·机器人·自动驾驶·agi·安全架构
Qres82114 小时前
算法复键——树状数组
数据结构·算法
H1785350909614 小时前
SolidWorks第四部分_直接实体建模特征9_替换面原理
线性代数·算法·机器学习·3d建模·solidworks
dayuOK630714 小时前
写作卡壳怎么办?我的“5分钟启动法”
人工智能·职场和发展·自动化·新媒体运营·媒体
不会就选b14 小时前
算法日常・每日刷题--<二分查找>3
算法