Ctrl K

Drop Adjacent Anagrams

Easy
You are given a list of lowercase words entries. While you can, repeat this step: pick a word that has a left neighbour which is its anagram (the same letters in the same quantities, possibly rearranged) and delete that word from the list. Return the list that remains once nothing more can be deleted. It can be shown that the result does not depend on the order of deletions.
Example 1
entries=['stop', 'pots', 'tops', 'spot', 'go', 'og', 'stop'] → ['stop', 'go', 'stop']

"pots", "tops" and "spot" each match their left neighbour letter for letter and get removed, then "og" goes the same way. The final "stop" stays: its left neighbour is now "go".

Example 2
entries=['ab', 'abb', 'bab'] → ['ab', 'abb']

"ab" and "abb" are not anagrams: they have a different number of b's. "bab" is an anagram of "abb", so it is removed.

Example 3
entries=['moon'] → ['moon']
Constraints
  • 1 ≤ len(entries) ≤ 10⁵
  • 1 ≤ len(entries[i]) ≤ 10
  • Words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTallest-First Roll Call EasyAnagram Buckets MediumA Subsequence of One Word Only MediumMost Searched Words MediumWord Grown Letter by Letter Medium
loading Python…
Input
entries=['stop', 'pots', 'tops', 'spot', 'go', 'og', 'stop']
Expected
['stop', 'go', 'stop']

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.