List All Permutations of an Alphanumeric String in Digit, Lowercase, Uppercase Order
Company: Wex
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Given a string `s` made of digits, lowercase letters and uppercase letters, return all permutations of `s`, sorted in a custom character order: digits come first, then lowercase letters, then uppercase letters.
### Function Signature
```python
def ordered_permutations(s: str) -> list[str]:
```
### Rules
- A permutation is a string that uses every character of `s` exactly as many times as it appears in `s`, in some order.
- Characters are ranked as follows: every digit ranks below every lowercase letter, and every lowercase letter ranks below every uppercase letter. Within a group the usual order applies: `0` to `9`, `a` to `z`, `A` to `Z`.
- Permutations are ordered by the first position at which they differ, using this rank. All permutations have the same length, so this order is total.
- If `s` contains repeated characters, each distinct permutation appears exactly once.
- This order is not the default string order: in ASCII, uppercase letters sort before lowercase letters.
### Constraints
- `1 <= len(s) <= 8`
- Every character of `s` is in `0-9`, `a-z` or `A-Z`.
- The output has at most `8! = 40,320` strings.
### Examples
**Example 1**
```text
Input: s = "aB1"
Output: ["1aB", "1Ba", "a1B", "aB1", "B1a", "Ba1"]
```
The characters rank `1`, then `a`, then `B`.
**Example 2**
```text
Input: s = "Ab"
Output: ["bA", "Ab"]
```
The lowercase `b` ranks below the uppercase `A`, so `"bA"` comes first, although default string comparison would put `"Ab"` first.
**Example 3**
```text
Input: s = "z0z"
Output: ["0zz", "z0z", "zz0"]
```
The two `z` characters are identical, so there are only three distinct permutations.
Overview: A coding problem that asks for every distinct permutation of a short string of digits, lowercase letters and uppercase letters, returned in a custom order where digits rank first, then lowercase, then uppercase. It tests permutation generation, duplicate handling and ordering that differs from default string comparison.
Given a string `s` made of digits, lowercase letters and uppercase letters, return all distinct permutations of `s`, sorted in a custom character order: digits come first, then lowercase letters, then uppercase letters.
Implement `ordered_permutations(s)`, which returns the permutations as a list of strings.
Rules:
- A permutation is a string that uses every character of `s` exactly as many times as it appears in `s`, in some order.
- Characters are ranked as follows: every digit ranks below every lowercase letter, and every lowercase letter ranks below every uppercase letter. Within a group the usual order applies: `0` to `9`, `a` to `z`, `A` to `Z`. A lowercase letter and its uppercase form (for example `a` and `A`) are different characters.
- Permutations are ordered by the first position at which they differ, using this rank, and the list goes from the lowest permutation to the highest. All permutations have the same length, so this order is total.
- If `s` contains repeated characters, each distinct permutation appears exactly once.
- This order is not the default string order: in ASCII, uppercase letters sort before lowercase letters. For example, for `s = "Ab"` the answer is `["bA", "Ab"]`, because the lowercase `b` ranks below the uppercase `A`.
No numeric value here comes close to 2^31 - 1.
Constraints:
- `1 <= len(s) <= 8`
- Every character of `s` is in `0-9`, `a-z` or `A-Z`; characters may repeat.
- The output has at most `8! = 40,320` strings.
Example 1:
Input: s = "aB1"
Output: ["1aB", "1Ba", "a1B", "aB1", "B1a", "Ba1"]
The characters rank `1`, then `a`, then `B`.
Example 2:
Input: s = "z0z"
Output: ["0zz", "z0z", "zz0"]
The two `z` characters are identical, so there are only three distinct permutations.
Constraints
- 1 <= len(s) <= 8
- Every character of s is in 0-9, a-z or A-Z; characters may repeat.
- The output has at most 8! = 40,320 strings.
Examples
Input: ('aB1',)
Expected Output: ['1aB', '1Ba', 'a1B', 'aB1', 'B1a', 'Ba1']
Explanation: Source example 1: one character from each group; ranks 1 < a < B.
Input: ('Ab',)
Expected Output: ['bA', 'Ab']
Explanation: Source example 2: lowercase b ranks below uppercase A, unlike ASCII order.
Hints
- Give every character a numeric rank (digits lowest, then lowercase, then uppercase) so that comparing characters in the custom order becomes comparing numbers.
- Default string comparison puts uppercase before lowercase, so do not rely on it for the final order.
- When s repeats a character, swapping two copies of it gives the same string; make sure each distinct permutation is produced only once.