Arrange Match Results in Repeating Win-Draw-Loss Order
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
# Arrange Match Results in Repeating Win-Draw-Loss Order
You are given a string containing only `W`, `D`, and `L`. Rearrange all characters into rounds. In each round, append at most one remaining `W`, then one remaining `D`, then one remaining `L`, skipping any character whose supply is exhausted. Continue until no characters remain.
Return the resulting string.
## Function Signature
```python
def arrange_results(results: str) -> str:
...
```
## Constraints
- `0 <= len(results) <= 1_000_000`
- Every character in `results` is one of `W`, `D`, or `L`.
- The output must contain exactly the same multiset of characters as the input.
## Examples
```text
Input: results = "WWWLLDDLD"
Output: "WDLWDLWDL"
```
```text
Input: results = "WLDDL"
Output: "WDLDL"
```
```text
Input: results = "WWWWLDDL"
Output: "WDLWDLWW"
```
Quick Answer: Rearrange a multiset of win, draw, and loss markers into repeated W-D-L rounds, skipping any marker once its supply is exhausted. The problem tests exact multiset preservation, deterministic round order, imbalanced supplies, empty input, and efficient construction for strings up to one million characters.
You are given a string `results` containing only the characters `W`, `D`, and `L` (win, draw, loss). Rearrange all of its characters into consecutive rounds. In each round, append at most one remaining `W`, then at most one remaining `D`, then at most one remaining `L`, skipping any letter whose supply is already exhausted. Continue producing rounds until no characters remain.
Return the resulting string. The answer is fully determined by the input: the output must contain exactly the same multiset of characters as `results`, arranged by the round rule above, so there is exactly one correct output for every input.
## Constraints
- `0 <= len(results) <= 1_000_000`
- Every character in `results` is one of `W`, `D`, or `L`.
- The output must contain exactly the same multiset of characters as the input.
## Examples
### Example 1
```text
Input: results = "WWWLLDDLD"
Output: "WDLWDLWDL"
```
The string has three of each letter, so three full `WDL` rounds are produced.
### Example 2
```text
Input: results = "WLDDL"
Output: "WDLDL"
```
Counts are `W=1`, `D=2`, `L=2`. Round 1 emits `W`, `D`, `L`. In round 2 the supply of `W` is exhausted, so it is skipped and the round emits `D`, `L`, giving `WDL` + `DL` = `WDLDL`.
### Example 3
```text
Input: results = "WWWWLDDL"
Output: "WDLWDLWW"
```
Counts are `W=4`, `D=2`, `L=2`. Rounds 1 and 2 each emit `WDL`; rounds 3 and 4 have only `W` left, each emitting a single `W`.
Constraints
- 0 <= len(results) <= 1_000_000
- Every character in results is one of W, D, or L.
- The output must contain exactly the same multiset of characters as the input.
Examples
Input: ('WWWLLDDLD',)
Expected Output: 'WDLWDLWDL'
Input: ('WLDDL',)
Expected Output: 'WDLDL'
Hints
- The original order of the characters never matters - only how many of each of the three letters the string contains.
- Process the answer as rounds: while anything remains, emit one W, then one D, then one L, skipping any letter whose remaining count is zero.
- At this input size, collect characters in a list or StringBuilder and join once at the end; repeated immutable-string concatenation can be quadratic.