Date: 2026-06-06
Time: 16:41
This file implements LeetCode 2273: Find Resultant Array After Removing Anagrams. It's one of hundreds of standalone solution modules in the leetcode-implementations repo, each owning a single problem's algorithm. Its sole responsibility is exporting anagramOperations, which filters consecutive anagrams from a word list.
anagramOperations(words: list[str]) -> list[str]The only public function. Contract:
Stack-style accumulation: The result list acts as a stack where each new word is compared against the top (result[-1]). This is a common LeetCode idiom for problems that require comparing each element to the most recently accepted one — similar to monotonic stack problems, but here the predicate is "is an anagram of."
Sorted-character canonicalization: Two strings are anagrams iff their sorted character sequences are equal. sorted(word) produces a canonical form for comparison. This avoids building a Counter or frequency array, trading a bit of performance (O(k log k) per word of length k) for brevity.
Imports: None — the solution uses only builtins (sorted, list).
Imported by: The test_solution.py in the same directory imports anagramOperations for testing. The massive "Imported By" list in the prompt is misleading — those are other problem directories' test files, not actual importers of this module. They likely share a common test harness pattern, not a direct dependency on this function.
1. Seed result with words[0] — the first word is always kept.
2. Iterate over words[1:].
3. For each word, sort its characters and compare against the sorted characters of result[-1] (the last accepted word).
4. If they differ (not anagrams), append the word to result.
5. If they match (anagrams), skip — the word is silently dropped.
6. Return result.
The comparison is always against the last *accepted* word, not the last *seen* word. This means if words = ["a", "a", "b", "a"], the second "a" is dropped (anagram of result[-1] = "a"), "b" is kept, and the final "a" is kept too (not an anagram of "b").
words[0] is accessed unconditionally. An empty list would raise IndexError.["ab", "cd", "ba"] returns ["ab", "cd", "ba"] — the "ba" survives because "cd" separates it from "ab".None. The function trusts its caller to provide a non-empty list of strings per the LeetCode problem constraints. An empty input crashes with an IndexError on words[0].