Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

899. Orderly Queue - Leetcode Solution

Code Implementation

class Solution:
    def orderlyQueue(self, s: str, k: int) -> str:
        if k == 1:
            # Only rotations are allowed, find the minimal rotation
            return min(s[i:] + s[:i] for i in range(len(s)))
        else:
            # Any permutation is possible, so just return sorted string
            return ''.join(sorted(s))
      
class Solution {
public:
    string orderlyQueue(string s, int k) {
        if (k == 1) {
            string res = s;
            for (int i = 1; i < s.size(); ++i) {
                string rotated = s.substr(i) + s.substr(0, i);
                if (rotated < res) res = rotated;
            }
            return res;
        } else {
            sort(s.begin(), s.end());
            return s;
        }
    }
};
      
class Solution {
    public String orderlyQueue(String s, int k) {
        if (k == 1) {
            String res = s;
            for (int i = 1; i < s.length(); ++i) {
                String rotated = s.substring(i) + s.substring(0, i);
                if (rotated.compareTo(res) < 0) res = rotated;
            }
            return res;
        } else {
            char[] arr = s.toCharArray();
            Arrays.sort(arr);
            return new String(arr);
        }
    }
}
      
var orderlyQueue = function(s, k) {
    if (k === 1) {
        let res = s;
        for (let i = 1; i < s.length; i++) {
            let rotated = s.slice(i) + s.slice(0, i);
            if (rotated < res) res = rotated;
        }
        return res;
    } else {
        return s.split('').sort().join('');
    }
};
      

Problem Description

You are given a string s and an integer k. You can perform the following operation any number of times: choose one of the first k letters of s and move it to the end of the string.

Your task is to return the lexicographically smallest string that can be obtained after applying this operation any number of times.

  • You must always select one of the first k letters for each move.
  • For k = 1, you may only move the first character to the end each time (i.e., rotate the string by one).
  • For k > 1, you may select any of the first k letters for each move, allowing for more flexibility.
  • Return only the lexicographically smallest possible string; do not reuse or duplicate characters.

Thought Process

Let's break down the problem by focusing on the value of k:

  • When k = 1: You can only move the first character to the end, which is equivalent to rotating the string. Therefore, the only possible strings you can obtain are all possible rotations of s. To find the answer, you need to check all rotations and pick the smallest one lexicographically.
  • When k > 1: You can move any of the first k characters to the end. With k = 2, for example, you could move either the first or the second character. If you think further, with enough moves, you can rearrange the string in any possible order (i.e., you can generate all permutations). That means, for k > 1, you can sort the string to get the smallest possible arrangement.

The main challenge is recognizing this difference and handling both cases efficiently.

Solution Approach

  • Step 1: Check the value of k
    • If k == 1, proceed to step 2.
    • If k > 1, proceed to step 3.
  • Step 2: For k == 1
    • Generate all possible rotations of the string s. For a string of length n, there are n rotations.
    • For each rotation, move the first character to the end and check if the resulting string is smaller than your current answer.
    • After checking all rotations, return the smallest one.
  • Step 3: For k > 1
    • Since any permutation is possible, simply sort the string's characters in ascending order.
    • Return the sorted string as the answer.
  • Why these choices?
    • For k == 1, the operation is limited, so brute-force through all rotations is necessary.
    • For k > 1, the operation is powerful enough to sort the string, so we take advantage of that for efficiency.

Example Walkthrough

Example 1:
s = "cba", k = 1

  • Possible rotations:
    • Original: cba
    • Rotate 1: bac
    • Rotate 2: acb
  • Out of these, acb is lexicographically smallest.
  • Return: acb

Example 2:
s = "baaca", k = 3

  • Since k > 1, we can sort the string.
  • Sort: a a a b c
  • Sorted string: aaabc
  • Return: aaabc

Time and Space Complexity

  • Brute-force Approach (k == 1):
    • There are n rotations for a string of length n.
    • Each rotation takes O(n) time to generate and compare.
    • Total Time: O(n^2)
    • Space: O(n) for storing rotations.
  • Optimized Approach (k > 1):
    • Sorting the string takes O(n \log n) time.
    • Space: O(n) for the sorted string.

The optimized approach is much faster for k > 1, while k == 1 is still efficient for reasonable string lengths.

Summary

The key insight for solving the "Orderly Queue" problem is recognizing how the value of k changes the set of possible strings you can generate. For k == 1, you are limited to rotations, so you must brute-force all possibilities. For k > 1, you can achieve any permutation, so simply sorting gives the answer. This dichotomy leads to a concise, elegant, and efficient solution that leverages both brute-force and optimal strategies depending on the input.