All problems
1180EasyArrayHash TableStringSorting

Clearing Rearranged Labels Off the Shelf

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2273Find Resultant Array After Removing Anagrams

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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