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
相关推荐
milanyangbo6 小时前
谁生?谁死?从引用计数到可达性分析,洞悉GC的决策逻辑
java·服务器·开发语言·jvm·后端·算法·架构
Swift社区6 小时前
LeetCode 409 - 最长回文串 | Swift 实战题解
算法·leetcode·swift
Lester_11018 小时前
嵌入式学习笔记 - 用泰勒公式解决 tanh函数
笔记·学习·算法
软件测试媛8 小时前
14:00开始面试,14:06就出来了,问的问题有点变态。。。
面试·职场和发展
无限进步_8 小时前
C语言字符串连接实现详解:掌握自定义strcat函数
c语言·开发语言·c++·后端·算法·visual studio
凤年徐8 小时前
HashMap 的哈希算法与冲突解决:深入 Rust 的高性能键值存储
算法·rust·哈希算法
J_Xiong01179 小时前
【VLNs篇】11:Dynam3D: 动态分层3D令牌赋能视觉语言导航中的VLM
人工智能·算法·3d
弈风千秋万古愁9 小时前
【PID】连续PID和数字PID chapter1(补充) 学习笔记
笔记·学习·算法·matlab
天选之女wow9 小时前
【代码随想录算法训练营——Day52】图论——101.孤岛的总面积、102.沉没孤岛、103.水流问题、104.建造最大岛屿
算法·深度优先·图论