Increasing Decreasing String
newbie · 275 · lc easy +19 · verified · 77.2% accepted · 840 likes · top 88%
Description
You are given a string s. Reorder it using the following algorithm:
- Remove the smallest character from s and append it to the result.
- Remove the next smallest character that is greater than the last appended character, and append it.
- Repeat step 2 until no character can be selected.
- Remove the largest remaining character and append it.
- Remove the next largest character that is smaller than the last appended character, and append it.
- Repeat step 5 until no character can be selected.
- Repeat steps 1 through 6 until all characters are placed.
If the smallest or largest character has duplicates, any copy may be chosen. Return the resulting string.
Example 1:
Example 2:
Code