Increasing Decreasing String

Easy

Description

Given a string s. Pick the smallest character, append it and remove it. Then pick the largest remaining, append and remove. Repeat alternating until empty.

Examples

Input:s = "aaaabbbbcccc"
Output:"abccbaabccba"
Explanation:

Alternating order.

Input:s = "a"
Output:"abccbaabccba"
Explanation:

A single-character string is processed by the alternating sort and reverse operations.

Input:s = "ddaabbcc"
Output:"abcddbca"
Explanation:

Start by picking smallest available chars: a, b, c, d. Then pick largest available: d, c, b, a. This demonstrates the alternating pattern of selecting minimum then maximum available characters until all are used.

Constraints

  • 1 ≤ s.length ≤ 500

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!