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
相关推荐
Cx330_FCQ1 小时前
Tmux使用
服务器·git·算法
拳里剑气2 小时前
C++算法:多源BFS
c++·算法·宽度优先·多源bfs
ysa0510303 小时前
【板子】短序列dp(换成维护更小常数维度的dp)
c++·笔记·算法·板子
shwill1233 小时前
PID 算法(三)--- 增量 PID ↔ 单神经元 PID 等价映射
linux·算法
wabs6664 小时前
关于图论【卡码网110.字符串迁移的思考】
数据结构·算法·图论
hanlin034 小时前
刷题笔记:力扣第242、349题(哈希表)
笔记·算法·leetcode
浮沉9875 小时前
二分查找算法例题(二)
算法
only-qi6 小时前
大模型Agent面试攻略:落地工程痛点、评估体系与Agentic RAG核心精讲
人工智能·算法·面试·职场和发展·langchain·rag
数字宝藏6 小时前
15本电子书分享
职场和发展·职场发展·教育电商
Gauss松鼠会8 小时前
【GaussDB】GaussDB锁阻塞源头查询
java·开发语言·前端·数据库·算法·gaussdb·经验总结