Find Resultant Array After Removing Anagrams - Problem
Find Resultant Array After Removing Anagrams

You are given a 0-indexed string array words, where each words[i] consists of lowercase English letters only.

Your task is to perform a cleanup operation repeatedly: select any index i where 0 < i < words.length and words[i - 1] and words[i] are anagrams, then delete words[i] from the array.

Keep performing this operation until no more adjacent anagram pairs exist. The beauty of this problem is that the order of operations doesn't matter - you'll always get the same final result!

What's an anagram? Two words are anagrams if one can be formed by rearranging the letters of the other using all letters exactly once. For example, "dacb" and "abdc" are anagrams.

Goal: Return the final array after all possible removals.

Input & Output

example_1.py โ€” Basic Case
$ Input: ["abba","baba","bbaa","cd","cd"]
โ€บ Output: ["abba","cd"]
๐Ÿ’ก Note: "baba" is an anagram of "abba", so remove it. "bbaa" is also an anagram of "abba", so remove it. The second "cd" is an anagram of the first "cd", so remove it. Final result: ["abba", "cd"]
example_2.py โ€” No Removals
$ Input: ["a","b","c"]
โ€บ Output: ["a","b","c"]
๐Ÿ’ก Note: No two adjacent words are anagrams of each other, so no words are removed. The array remains unchanged.
example_3.py โ€” All Same Anagrams
$ Input: ["a","a","a"]
โ€บ Output: ["a"]
๐Ÿ’ก Note: All words are identical (and thus anagrams). We keep the first one and remove all subsequent identical words.

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 10
  • words[i] consists of lowercase English letters only
  • Order of operations doesn't affect the final result

Visualization

Tap to expand
๐Ÿ“š Library Book Organization ProcessOriginal ShelfABBABABABBAACDCDSignatures (sorted letters):AABBAABBAABBCDCDDecision Process:โœ“KEEPโœ—REMOVEโœ—REMOVEโœ“KEEPโœ—REMOVEFinal Clean ShelfABBACDOne Pass Algorithm: O(n ร— m log m) Time Complexityโœจ Key Insight: Only compare each book with the LAST book you decided to keep!No need to repeatedly scan the entire shelf - one pass is enough!
Understanding the Visualization
1
Start with First Book
Always keep the first book as your reference
2
Create Signatures
For each book, create a signature by sorting its title letters
3
Compare and Decide
If the signature matches the last kept book, remove it; otherwise keep it
4
Update Reference
When keeping a book, update your reference signature
Key Takeaway
๐ŸŽฏ Key Insight: Instead of repeatedly scanning for adjacent anagrams, build the result array in one pass by comparing each word's signature only with the last word you decided to keep. This transforms an O(nยฒ) repeated-scan approach into an optimal O(n ร— m log m) single-pass solution!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen