Problem · String

Lexicographically Smallest Task Queue

MediumAgodaCONTRACTOA

A system manages a queue of tasks represented by a string taskQueue. Each character is one of:

  • '1': low priority
  • '2': medium priority
  • '3': high priority

You may perform either of the following adjacent swaps any number of times:

  • swap adjacent tasks with values '1' and '2'
  • swap adjacent tasks with values '2' and '3'

Return the lexicographically smallest task order that can be obtained.

A task order a is lexicographically smaller than task order b if, at the first position where they differ, a has the smaller priority character.

Examples
01 · Example 1
taskQueue = "13212"
return = "12231"

The two '2' tasks can move before the first '3', but the relative order of the '1' and '3' tasks cannot be changed directly. The smallest reachable order is "12231".

Constraints
  • 1 <= taskQueue.length
  • taskQueue contains only '1', '2', and '3'.
More Agoda problems
drafts saved locally
public String getSmallestTaskQueue(String taskQueue) {
  // write your code here
}
taskQueue"13212"
expected"12231"
sign in to submit