Increasing Decreasing String Problem
Description
LeetCode Problem 1370.
You are given a string s. Reorder the string using the following algorithm:
- Remove the smallest character from
sand append it to the result. - Remove the smallest character from
sthat 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
sand append it to the result. - Remove the largest character from
sthat 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
shave 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:
Input: s = "aaaabbbbcccc"
Output: "abccbaabccba"
Explanation: After steps 1, 2 and 3 of the first iteration, result = "abc"
After steps 4, 5 and 6 of the first iteration, result = "abccba"
First iteration is done. Now s = "aabbcc" and we go back to step 1
After steps 1, 2 and 3 of the second iteration, result = "abccbaabc"
After steps 4, 5 and 6 of the second iteration, result = "abccbaabccba"
Example 2:
Input: s = "rat"
Output: "art"
Explanation: The word "rat" becomes "art" after re-ordering it with the mentioned algorithm.
Constraints:
1 <= s.length <= 500sconsists of only lowercase English letters.
Solution
Approach: Count Letters, Sweep Up and Down
The algorithm only ever asks for "the smallest remaining letter bigger than the last one" or "the largest remaining letter smaller than the last one", and which copy of a letter is used does not matter. So only the count of each letter matters.
Steps 1 to 3 together take each letter that is still left once, in order from a to z. Steps 4 to 6 take each remaining letter once from z down to a. So:
- Count the 26 letters.
- Repeat until the result is as long as
s: sweepcfromatoz, appendingcand decrementing its count whenever the count is positive; then sweep fromztoathe same way.
In Example 1, the counts are a: 4, b: 4, c: 4. The upward sweep appends abc, the downward sweep cba, leaving 2 of each; the next round appends abccba again.
Note that a letter can appear twice in a row at the turn of a sweep, as the cc in abccba shows: the upward sweep ends with the largest letter, and the downward sweep starts with the largest letter that is still left, which may be the same one.
Complexity: O(26 * r + n) time, where r is the number of rounds (at most the highest count of a letter, so at most n) and n the length of s; O(26) extra space for the counts besides the result.
class Solution:
def sortString(self, s: str) -> str:
count = [0] * 26
for ch in s:
count[ord(ch) - ord("a")] += 1
# One round: each remaining letter once from a to z, then once from z to a.
order = list(range(26)) + list(range(25, -1, -1))
ans = []
while len(ans) < len(s):
for c in order:
if count[c] > 0:
ans.append(chr(ord("a") + c))
count[c] -= 1
return "".join(ans)