Skip to content

Latest commit

 

History

History
81 lines (42 loc) · 1.03 KB

File metadata and controls

81 lines (42 loc) · 1.03 KB

Codeforces – 339A. Helpful Maths

Problem Statement

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.

Description

  • Only digits 1, 2, 3 appear.

  • Digits are separated by "+".

  • You must sort digits and reconstruct the string.

Input

One string:

"digit+digit+digit..."

Output

Sorted version of the sum, like:

1+2+3

Examples

Example 1

Input:

3+2+1

Output:

1+2+3
Explanation:
Extract digits → 3, 2, 1 → sort → 1, 2, 3.

Example 2

Input:

1+1+3+1

Output:

1+1+1+3

Constraints

  • String length ≤ 100

  • Only characters allowed: '1', '2', '3', '+'

  • At least one number exists

Hints

  • Split string using "+"

  • Sort results

  • Join using "+"

Approach / Explanation

  1. Split by "+"

  2. Sort digits

  3. Rejoin with "+"

  4. Print result

Complexity

  • Time: O(n log n)