Show problems as
Classic problem #2273EasyArrayHash TableStringSorting

Find Resultant Array After Removing Anagrams

Real-world version: Clearing Rearranged Labels Off the Shelf
Tracked in this browser only
Read Find Resultant Array After Removing Anagrams on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A shelf holds the labels labels in order. While some label is a rearrangement of the label directly before it, the later of the two is taken off and the shelf closes up.

Return the labels that remain, in order. Whichever such pair is dealt with first, the shelf ends up the same.

Examples

Example 1

Input
labels = ["abc", "bca", "cab", "xyz"]
Output
["abc", "xyz"]

The second and third labels both rearrange the first, so both come off, and the last shares none of its letters.

Example 2

Input
labels = ["a", "a", "b", "b", "a"]
Output
["a", "b", "a"]

The repeats standing next to each other come off, but the final a follows a b so it stays.

Example 3

Input
labels = ["ab", "cd"]
Output
["ab", "cd"]

Neither label rearranges the other, so the shelf is untouched.

Constraints

  • 1 <= labels.length <= 100
  • 1 <= labels[i].length <= 10
  • each label holds only lowercase English letters

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def remove_anagrams(labels: list[str]) -> list[str]:
Java
public List<String> removeAnagrams(String[] labels)