Digit Start Permutations
MediumBacktracking · Walmart
You are given a string s made of lowercase letters and digits. Return every distinct rearrangement of all the characters of s whose first character is a digit.
- Each result uses every character of
sexactly once. - Characters may repeat in
s; rearrangements that produce the same string are listed once. - Return the strings sorted in ascending order (normal string comparison, where digits come before letters).
- If
scontains no digit, return an empty list.
Example 1
- Input:
- s = "a1b1"
- Output:
- ["11ab", "11ba", "1a1b", "1ab1", "1b1a", "1ba1"]
- Explanation
- Only
1can lead. The remaininga,b,1are distinct, giving 3! = 6 strings. Swapping the two1s does not create new strings, so nothing is listed twice.
Example 2
- Input:
- s = "21a"
- Output:
- ["12a", "1a2", "21a", "2a1"]
- Explanation
- Strings starting with
1come first, then those starting with2; each leader is followed by both orders of the other two characters.
Example 3
- Input:
- s = "xyz"
- Output:
- []
- Explanation
- No rearrangement can start with a digit.
Constraints
1 <= s.length <= 8scontains only lowercase English letters and digits