You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string.
Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
Example 1:
Input: s = "cba", k = 1 Output: "acb" Explanation: In the first move, we move the 1st character 'c' to the end, obtaining the string "bac". In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".
Example 2:
Input: s = "baaca", k = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab". In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".
Constraints:
1 <= k <= s.length <= 1000s consist of 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:
The brute force approach to this problem involves exploring every possible reordering of the input string. We essentially try every shift possible and pick the lexicographically smallest one. This guarantees we find the absolute best arrangement.
Here's how the algorithm would work step-by-step:
def orderly_queue_brute_force(input_string, queue_length):
# If k > 1, we can always sort the string
if queue_length > 1:
return ''.join(sorted(input_string))
smallest_string = input_string
# Try every possible rotation of the string
for shift_amount in range(len(input_string)):
rotated_string = input_string[shift_amount:] + input_string[:shift_amount]
# Compare the current rotation with the smallest one found so far
if rotated_string < smallest_string:
smallest_string = rotated_string
return smallest_stringThe problem asks to find the lexicographically smallest string you can make by repeatedly moving a character from the beginning to the end. The key insight is that if you can move more than one character at a time, you can simply sort the string to get the answer.
Here's how the algorithm would work step-by-step:
def orderly_queue(input_string, k_moves):
string_length = len(input_string)
# If k > 1, we can sort
if k_moves > 1:
sorted_string = ''.join(sorted(input_string))
return sorted_string
#If only one move allowed, find the smallest string by rotations.
smallest_string = input_string
for i in range(string_length):
rotated_string = input_string[1:] + input_string[:1]
#Update smallest_string if we find a lexicographically smaller arrangement
if rotated_string < smallest_string:
smallest_string = rotated_string
input_string = rotated_string
return smallest_string| Case | How to Handle |
|---|---|
| Empty input string | Return an empty string since there's nothing to process. |
| k = 1 | Rotate the string all possible ways and compare lexicographically. |
| k > 1 | The string can be sorted to get the lexicographically smallest string. |
| String with one character | Return the same string since no operation changes it. |
| String with duplicate characters | The algorithm will still perform the rotations or sorting to find the lexicographically smallest, regardless of duplicates. |
| Large string size (performance) | For k > 1, sorting the string is O(n log n), which is more efficient than rotating for very large strings, while k=1 iterates n times through n chars, being O(n^2). |
| String with all same characters | For k = 1, the algorithm will still rotate the string and compare, and for k > 1, sorting will result in the same string. |
| Null input string | Throw an IllegalArgumentException or return null to indicate an invalid input. |