Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Increasing Decreasing String

XPChallenge Points: 10
levelLevel: Easy

You are given a string s, and your task is to rearrange its characters following a repeated “up and down” selection process.

The process works as follows:

  • First, take characters in increasing alphabetical order, each time picking the smallest character greater than the previous appended one.

  • When no more characters can be chosen in increasing order, switch direction.

  • Now pick characters in decreasing alphabetical order, each time choosing the largest character smaller than the last appended one.

  • Alternate between ascending and descending phases until all characters in s have been used.

Whenever multiple copies of a character exist, any instance can be used.

Return the final string produced after applying this pattern.

Example 1:

Input: s = "bbccaa"

Output: "abcacb"

Explanation:

Characters first rise ("abc"), then fall ("acb"), continuing until all are used.

Example 2:

Input: s = "ddddcccbbba"

Output: "abcdcbadcdc"

Explanation:

Alternating smallest-to-largest and largest-to-smallest builds the final pattern.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result