Increasing Decreasing String
easy · verified · 77.2% accepted · 840 likes · top 88%
hash table · string · counting
Description
You are given a string s. Reorder the string using the following algorithm:
- Remove the smallest character from s and append it to the result.
- Remove the smallest character from s that is greater than the last appended character, and append it to the result.
- Repeat step 2 until no more characters can be removed.
- Remove the largest character from s and append it to the result.
- Remove the largest character from s that is smaller than the last appended character, and append it to the result.
- Repeat step 5 until no more characters can be removed.
- Repeat steps 1 through 6 until all characters from s have been removed.
If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.
Return the resulting string after reordering s using this algorithm.
Example 1:
Example 2:
Solution