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
相关推荐
童园管理札记20 小时前
【续】数字时代:学前教育的新改革
经验分享·深度学习·职场和发展·微信公众平台
CN-Dust21 小时前
【C++】while语句例题专题
数据结构·c++·算法
灵智实验室21 小时前
PX4位置速度估计技术详解(四):LPE 激光雷达高度融合的实现错误
算法·无人机·px 4
CQU_JIAKE1 天前
【A】3742,3387,并查集
算法
gihigo19981 天前
CHAN时延估计算法(二维/三维定位实现)
算法
freexyn1 天前
Matlab自学笔记七十六:表达式的展开、因式分解、化简、合并同类项
笔记·算法·matlab
样例过了就是过了1 天前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
迦南的迦 亚索的索1 天前
AI_11_Coze_AI面试助手
人工智能·面试·职场和发展
dog2501 天前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
童园管理札记1 天前
数字时代:学前教育的新改革
经验分享·职场和发展·学习方法·微信公众平台