You are given a string s and a robot that currently holds an empty string t. Apply one of the following operations until s and t are both empty:
s and give it to the robot. The robot will append this character to the string t.t and give it to the robot. The robot will write this character on paper.Return the lexicographically smallest string that can be written on the paper.
Example 1:
Input: s = "zza" Output: "azz" Explanation: Let p denote the written string. Initially p="", s="zza", t="". Perform first operation three times p="", s="", t="zza". Perform second operation three times p="azz", s="", t="".
Example 2:
Input: s = "bac" Output: "abc" Explanation: Let p denote the written string. Perform first operation twice p="", s="c", t="ba". Perform second operation twice p="ab", s="c", t="". Perform first operation p="ab", s="", t="c". Perform second operation p="abc", s="", t="".
Example 3:
Input: s = "bdda" Output: "addb" Explanation: Let p denote the written string. Initially p="", s="bdda", t="". Perform first operation four times p="", s="", t="bdda". Perform second operation four times p="addb", s="", t="".
Constraints:
1 <= s.length <= 105s consists of only English lowercase 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:
Imagine you have a jumbled sequence of letters and you want to build the alphabetically smallest possible word by moving letters between a starting pile, a temporary holding shelf, and your final output. The brute force approach involves exploring every single possible sequence of moves to see what final words they create.
Here's how the algorithm would work step-by-step:
def robot_print_lexicographically_smallest(source_string):
all_generated_words = []
def explore_all_move_paths(source_index, holding_shelf, current_output):
if source_index == len(source_string) and not holding_shelf:
all_generated_words.append("".join(current_output))
return
# Explore taking a letter from the shelf first if possible to keep early letters small
if holding_shelf:
cloned_shelf = list(holding_shelf)
letter_from_shelf = cloned_shelf.pop()
current_output.append(letter_from_shelf)
explore_all_move_paths(source_index, cloned_shelf, current_output)
current_output.pop()
cloned_shelf.append(letter_from_shelf)
# Explore taking the next letter from the source pile and putting it onto the shelf
if source_index < len(source_string):
holding_shelf.append(source_string[source_index])
explore_all_move_paths(source_index + 1, holding_shelf, current_output)
holding_shelf.pop()
explore_all_move_paths(0, [], [])
# Sort all completed paths to find the ultimate lexicographically smallest resulting word
all_generated_words.sort()
return all_generated_words[0]To build the alphabetically smallest possible string, we use a holding area to temporarily store letters while deciding whether to print them now or wait for better options. By looking ahead at what letters are still waiting to be processed, we can make the smartest decision at every single moment.
Here's how the algorithm would work step-by-step:
def robot_print_lexicographically_smallest_string(input_string):
from collections import Counter
letter_frequencies = Counter(input_string)
holding_stack = []
output_characters = []
minimum_available_char = 'a'
for current_incoming_char in input_string:
holding_stack.append(current_incoming_char)
letter_frequencies[current_incoming_char] -= 1
while minimum_available_char <= 'z' and letter_frequencies[minimum_available_char] == 0:
minimum_available_char = chr(ord(minimum_available_char) + 1)
# Pop from stack if top element is smaller or equal to the minimum remaining incoming letter.
while holding_stack and holding_stack[-1] <= minimum_available_char:
output_characters.append(holding_stack.pop())
# Empty the remaining holding stack into output once all incoming letters are processed.
while holding_stack:
output_characters.append(holding_stack.pop())
return "".join(output_characters)| Case | How to Handle |
|---|---|
| Empty string input | Return an empty string immediately as no operations are needed. |
| String with a single character | Append the character to the paper string and immediately pop it to the result string. |
| String with all identical characters | Push all characters onto the paper string and then pop them all off in reverse or standard order. |
| Strictly decreasing string such as cba | Keep pushing until the end, then pop everything in reverse to achieve the lexicographically smallest result abc. |
| Strictly increasing string such as abc | Pop characters immediately as they arrive since no smaller future characters will appear. |
| Maximum length string scaling up to constraints | Use a precomputed suffix minimum frequency array to ensure linear time complexity without exceeding time limits. |
| String with alternating small and large characters | Greedily pop from the paper string only when the top element is smaller than or equal to the minimum remaining character in the input. |
| Memory constraints with deep string manipulations | Use a StringBuilder in Java or list appends in Python to construct the output efficiently without excessive garbage collection. |