You are given a string like "3+2+1" which represents the sum of digits.
Rearrange the digits in non-decreasing order and output them in the same sum format.
-
Only digits
1,2,3appear. -
Digits are separated by
"+". -
You must sort digits and reconstruct the string.
Input
One string:
"digit+digit+digit..."
Sorted version of the sum, like:
1+2+3
Input:
3+2+1
Output:
1+2+3
Explanation:
Extract digits → 3, 2, 1 → sort → 1, 2, 3.
Input:
1+1+3+1
Output:
1+1+1+3
-
String length ≤ 100
-
Only characters allowed:
'1','2','3','+' -
At least one number exists
-
Split string using
"+" -
Sort results
-
Join using
"+"
-
Split by
"+" -
Sort digits
-
Rejoin with
"+" -
Print result
Complexity
- Time:
O(n log n)