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
相关推荐
中國龍在廣州2 分钟前
“物理AI”吹响号角
大数据·人工智能·深度学习·算法·机器人·机器人学习
悄悄敲敲敲2 分钟前
MySQL复合查询
面试·职场和发展
CCPC不拿奖不改名4 分钟前
面向对象编程:继承与多态+面试习题
开发语言·数据结构·python·学习·面试·职场和发展
꧁Q༒ོγ꧂6 分钟前
算法详解(二)--算法思想基础
java·数据结构·算法
꧁Q༒ོγ꧂7 分钟前
算法详解(一)--算法系列开篇:什么是算法?
开发语言·c++·算法
橘颂TA7 分钟前
【剑斩OFFER】算法的暴力美学——力扣:1047 题:删除字符串中的所有相邻重复项
c++·算法·leetcode·职场和发展·结构于算法
2301_800256117 分钟前
R-Tree创建与遍历,R-Tree在4类空间查询中的应用,实现4类空间查询的各类算法[第8章]
数据库·算法·机器学习·postgresql·r-tree
Dontla13 分钟前
Mock Interview模拟面试,20251225,MNC第一面HR面,AI Engineer
职场和发展
BFT白芙堂14 分钟前
基于 GPU 并行加速的 pRRTC 算法:赋能 Franka 机械臂的高效、稳定运动规划
人工智能·深度学习·算法·机器学习·gpu·具身智能·frankaresearch3
牛老师讲GIS14 分钟前
多边形简化讲解:从四大核心算法到 Mapshaper 自动化实战
网络·算法·自动化