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.
Input: s = "bbccaa"
Output: "abcacb"
Explanation:
Characters first rise ("abc"), then fall ("acb"), continuing until all are used.
Input: s = "ddddcccbbba"
Output: "abcdcbadcdc"
Explanation:
Alternating smallest-to-largest and largest-to-smallest builds the final pattern.
Accepted:
Submission: