Find Resultant Array After Removing Anagrams
Real-world version: Clearing Rearranged Labels Off the ShelfThe 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.
def remove_anagrams(labels: list[str]) -> list[str]:public List<String> removeAnagrams(String[] labels)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's