LeetCode Problem

How to Solve Orderly Queue

The key to solving Orderly Queue is understanding that when k equals 1, only rotations are allowed, requiring careful comparison of all cyclic permutations. When k is greater than 1, the problem reduces to sorting the string because multiple moves can simulate any ordering. Using this distinction, you can quickly determine the minimal lexicographic string with either rotation checks or full sorting.

GhostInterview Help

Need help with Orderly Queue without spending extra time grinding it?

GhostInterview can read Orderly Queue from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #899Math plus StringReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Math plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The key to solving Orderly Queue is understanding that when k equals 1, only rotations are allowed, requiring careful comparison of all cyclic permutations. When k is greater than 1, the problem reduces to sorting the string because multiple moves can simulate any ordering. Using this distinction, you can quickly determine the minimal lexicographic string with either rotation checks or full sorting.

Problem Statement

You are given a string s and an integer k. You may choose one of the first k letters of s and move it to the end of the string any number of times. The goal is to transform s into the lexicographically smallest string possible through repeated applications of this operation.

Return the lexicographically smallest string obtainable after applying the allowed moves. For example, if s = "cba" and k = 1, repeated rotations produce "acb". If s = "baaca" and k = 3, strategic moves yield "aaabc". All input strings consist of lowercase English letters, and 1 <= k <= s.length.

Examples

Example 1

Input: s = "cba", k = 1

Output: "acb"

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"

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 <= 1000
  • s consist of lowercase English letters.

Solution Approach

Handle k equals 1 with rotations

When k is 1, only rotations are allowed, so generate all cyclic permutations of s by repeatedly moving the first character to the end. Compare each rotation lexicographically and select the smallest one as the answer.

Handle k greater than 1 with sorting

If k > 1, the problem simplifies because any sequence of moves can simulate a bubble sort, allowing characters to rearrange freely. Sort the characters of s in ascending order to obtain the lexicographically smallest string efficiently.

Optimize for combined patterns

For strings where k = 1 but length is small, compare all rotations directly. For larger strings or k > 1, apply a single sorted transformation. This approach balances string manipulation with mathematical analysis of rotations and possible moves.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

For k = 1, generating all n rotations takes O(n^2) time and O(n) space per rotation comparison. For k > 1, sorting the string takes O(n log n) time and O(n) space. Overall, complexity depends on k and string length n.

What Interviewers Usually Probe

  • Check if the candidate recognizes the special case when k = 1 versus k > 1.
  • Notice if the candidate efficiently compares rotations without redundant string copies.
  • Evaluate whether the candidate leverages sorting when k > 1 to reduce operations.

Common Pitfalls or Variants

Common pitfalls

  • Assuming k > 1 can be treated like k = 1 and only rotating characters.
  • Overlooking that k = 1 requires considering all cyclic permutations.
  • Performing unnecessary operations for k > 1 instead of direct sorting.

Follow-up variants

  • Allowing multiple character moves at once instead of only one within the first k.
  • Finding the lexicographically largest string instead of the smallest.
  • Restricting moves to only the first character regardless of k value.

How GhostInterview Helps

  • GhostInterview identifies whether k = 1 or k > 1 and suggests the optimal approach instantly.
  • It computes all cyclic rotations for k = 1 and compares them efficiently to avoid manual mistakes.
  • For k > 1, it directly generates the sorted minimal string, saving time and reinforcing the pattern recognition.

Topic Pages

FAQ

What is the main pattern in the Orderly Queue problem?

The problem combines Math with String manipulation, analyzing rotations and sorting to reach the minimal lexicographic string.

How do I solve Orderly Queue when k equals 1?

Generate all cyclic rotations of the string by moving the first character repeatedly, then pick the smallest rotation lexicographically.

How do I solve Orderly Queue when k is greater than 1?

Sort the string in ascending order since repeated moves allow any character ordering, guaranteeing the smallest lexicographic result.

What are common mistakes in this problem?

Ignoring the k = 1 rotation restriction, performing unnecessary operations for k > 1, or not comparing rotations correctly.

Can this approach scale to large strings?

Yes, for k > 1 sorting is efficient with O(n log n), but for k = 1 each rotation check is O(n^2), so optimization is needed for large n.

GhostInterview Solver

Need direct help with Orderly Queue instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Orderly Queue from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.