Arrange Garment Sizes in Standard Order
StringsAuto-graded · 22 test casesLast updated
Tests understanding of counting sort for a fixed alphabet, achieving linear time complexity instead of comparison-based sorting.
You receive a string garments containing only the characters 'S', 'M', and 'L', which represent garment sizes (Small, Medium, Large). Rearrange the string so that all Small sizes appear first, followed by all Medium sizes, and finally all Large sizes.
Write a function arrange_garment_sizes(garments: str) -> str that returns the rearranged string.
Constraints
- The input string length is between 0 and 1,000,000 characters
- The string contains only the characters
'S','M', and'L' - Your solution should run in O(n) time where n is the string length
- Use O(1) auxiliary space (the output string itself is not counted)
Examples
Example 1
Input: arrange_garment_sizes('LMSMS')
Output: 'SSMML'
Example 2
Input: arrange_garment_sizes('MMM')
Output: 'MMM'
Hints
Hint 1
With only three distinct values, do you really need a general-purpose sorting algorithm?
Hint 2
Count how many times each character appears, then reconstruct the result.
Hint 3
This is counting sort with a three-element alphabet—the counts array takes constant space.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When the input contains only three distinct values, a full comparison-based sort is unnecessary. Instead, we use counting sort tailored to a fixed alphabet.
The algorithm works in two phases:
- Count each character: Make a single pass through the input string, maintaining three counters for
'S','M', and'L'. - Reconstruct the output: Build the result by concatenating the appropriate number of each character in the desired order.
Since we have exactly three possible characters, the counting array uses constant space regardless of input size.
Why This Works
The output must contain exactly the same characters as the input, just rearranged in ascending order (S < M < L). For a string over a three-character alphabet, the sorted arrangement is uniquely determined by how many of each character it contains. By counting accurately and emitting characters in priority order, we produce the one correct sorted permutation.
Edge cases handle naturally:
- Empty strings produce zero counts, yielding an empty result
- Strings with only one character type produce two zero counts and reproduce the input
- Already-sorted or reverse-sorted inputs require no special handling
Complexity
- Time: O(n) where n is the string length. We scan the input once to count and perform constant-time string concatenation.
- Space: O(1) auxiliary space. We maintain three integer counters. The output string takes O(n) space but is required by the problem.
Solution
def arrange_garment_sizes(garments: str) -> str:
s_count = 0
m_count = 0
l_count = 0
for char in garments:
if char == 'S':
s_count += 1
elif char == 'M':
m_count += 1
else:
l_count += 1
return 'S' * s_count + 'M' * m_count + 'L' * l_count
For the input "LMSMS":
- Counting gives: S=2, M=2, L=1
- Reconstruction produces:
"SS"+"MM"+"L"="SSMML"