Skip to main content
Back to problems
Codeforces
Easy
Arrays
Strings
Sorting
Helpful Maths

Reorder the terms in an addition expression so they appear in nondecreasing order.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given a simple arithmetic expression containing only single-digit positive integers separated by plus signs, such as 3+2+1. Your task is to rearrange the numbers so that the expression is sorted in nondecreasing order.

The + signs must remain between every pair of numbers, and the final result should contain the same numbers as the input, just in sorted order.

Goal

Print the expression after sorting all numbers from smallest to largest.

Input Format

  • A single string s representing an expression of the form a+b+c+...
  • Each term is a single digit from 1 to 3 or more generally a positive integer digit in the original problem style
  • Terms are separated by the character +

Output Format

  • Print one string: the same numbers arranged in nondecreasing order and joined by +

Constraints

  • The expression contains at least one number
  • All numbers are valid positive integers in the original problem format
  • You may assume the input is well-formed
Examples
Sample cases returned by the problem API.

Example 1

Input

3+2+1

Output

1+2+3

Explanation

The numbers are rearranged into increasing order.

Example 2

Input

1+1+3+1

Output

1+1+1+3

Explanation

All terms are preserved, only their order changes.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.