Distinct Arrangements in Order
word is a string of lowercase letters that may contain repeated letters. Return every distinct string that can be made by rearranging all of its letters, each exactly once, listed in alphabetical order. Rearrangements that only swap equal letters produce the same string, and that string must appear only once. The empty string has one arrangement, itself, so "" gives [""].
The recursive approach: sort the letters, choose each distinct letter in turn as the first character, and attach every arrangement of the remaining letters.
For example, "aab" gives ["aab", "aba", "baa"].
Example 1
Starting with a leaves ab, giving aab and aba; starting with b leaves aa, giving baa. The second a would only repeat the first two.
Example 2
Two different letters give two arrangements, ab before ba.
Constraints
0 <= len(word) <= 6wordhas lowercase letters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Recursion problems
- Every Route Through the GardenMedium
- Fair SplitMedium
- Fill In the BracketsMedium
- Hidden Word CountMedium
- Maze EscapeMedium
- Numbers RoundMedium
- Tower MovesMedium
- Tower With a Middle StopMedium