You are given a string s. Reorder the string using the following algorithm:
s and append it to the result.s that is greater than the last appended character, and append it to the result.s and append it to the result.s that is smaller than the last appended character, and append it to the result.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:
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 <= 500s consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This simulation counts how many times each lowercase letter appears. It then repeatedly takes one remaining copy of each letter from a to z, then one remaining copy from z to a. It stops when the built text is as long as the original text.
Here's how the algorithm would work step-by-step:
class Solution:
def sortString(self, input_string: str) -> str:
character_counts = [0] * 26
for char in input_string:
character_counts[ord(char) - ord('a')] += 1
result_list = []
total_characters = len(input_string)
# Loop until every character from the original string has been placed in the result.
while len(result_list) < total_characters:
# This ascending pass picks the smallest available characters in increasing order.
for index in range(26):
if character_counts[index] > 0:
result_list.append(chr(ord('a') + index))
character_counts[index] -= 1
# This descending pass is necessary to pick the largest available characters.
for index in range(25, -1, -1):
if character_counts[index] > 0:
result_list.append(chr(ord('a') + index))
character_counts[index] -= 1
# After all passes, the list of characters must be joined into the final string.
return "".join(result_list)This version also starts from letter counts rather than from repeated searches through the raw text. It drains those counts with an ascending pass and a descending pass, appending one available letter at a time. The result is complete when its length matches the input length.
Here's how the algorithm would work step-by-step:
def sort_string(input_string):
character_counts = [0] * 26
for character in input_string:
character_counts[ord(character) - ord('a')] += 1
result_characters = []
total_characters = len(input_string)
# The loop must continue until all characters from the input have been placed in the result.
while len(result_characters) < total_characters:
# First pass: Ascending order from 'a' to 'z'.
for character_index in range(26):
if character_counts[character_index] > 0:
result_characters.append(chr(ord('a') + character_index))
character_counts[character_index] -= 1
# Second pass: Descending order from 'z' to 'a'.
for character_index in range(25, -1, -1):
if character_counts[character_index] > 0:
result_characters.append(chr(ord('a') + character_index))
character_counts[character_index] -= 1
# The list of characters is joined to form the final sorted string.
return "".join(result_characters)| Case | How to Handle |
|---|---|
| The text is a single character | Return that character, because the first ascending pass takes it and nothing remains. |
| Every character is the same letter | Alternate that letter between the ascending and descending passes until its count is exhausted. |
| The text is already strictly increasing | The first ascending pass emits every character and the descending pass adds nothing. |
| Some letters are already used up in the middle of a pass | Skip any letter whose remaining count is zero and continue with the next letter in that direction. |
| The text repeats several distinct letters the same number of times | Each full pair of passes emits two copies of every remaining letter until the counts run out. |