↓Skip to main content
  1. LeetCode/

LeetCode 2273: Find Resultant Array After Removing Anagrams

·2 mins· ·
LeetCode Easy Hash-Table String Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-29

  • Submissions: Accepted (first try)

Approach
#

After deleting words[i], the next word’s “previous” becomes words[i - 1]. The deleted word was an anagram of words[i - 1], and anagrams are transitive, so comparing with either gives the same result.

So scan left to right and compare only with the last kept word: keep it if not an anagram. Check anagrams with sorted strings (Hash Table Template 5).

My Solution
#

I originally went right to left, comparing neighbors with Counter and popping when equal. Here it builds a new list left to right:

class Solution:
    def removeAnagrams(self, words: List[str]) -> List[str]:
        res = []
        for w in words:
            if not res or sorted(res[-1]) != sorted(w):
                res.append(w)
        return res

Complexity
#

Time O(n · L log L) for word length L, space O(n · L).

Mistakes to Avoid
#

  • pop inside a loop shifts indices; it is only safe right to left, and building a new list is simpler.

Interview Explanation
#

After a deletion the next word compares with the one before the deleted word; anagrams are transitive, so that equals comparing with the last kept word. I scan left to right and keep a word when its sorted form differs from the last kept one. Time O(n L log L).

Related

LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 3606: Coupon Code Validator
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 3606 note: filter valid coupons, then sort by (category order, code); character ranges must include z, Z and 9
LeetCode 389: Find the Difference
·1 min
LeetCode Easy Hash-Table String Bit-Manipulation Sorting
LeetCode 389 note: t has one extra character; XOR every character of both strings, pairs cancel, and the extra one remains
LeetCode 242: Valid Anagram
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 242 note: two strings are anagrams exactly when their character counts match; compare Counters or sorted strings
LeetCode 3803: Count Residue Prefixes
·1 min
LeetCode Easy Hash-Table String
LeetCode 3803 note: add characters to a set while scanning; count a prefix when its distinct count equals its length % 3
LeetCode 3090: Maximum Length Substring With Two Occurrences
·1 min
LeetCode Easy Sliding-Window Hash-Table String
LeetCode 3090 note: a longest-window slide that shrinks the left whenever the newly added character appears more than twice