Data Structures & AlgorithmsHard

Using only swaps of ADJACENT characters, what is the minimum number of swaps needed to move ALL letters 'x' of "bxaxxbx" to the very front of the string (order of the other letters does not matter)?

  1. A. 9
  2. B. 4
  3. C. 8
  4. D. 2

Correct answer: C. 8

Each non-x letter that sits before some x must be crossed by that x, costing one swap per crossing. A breadth-first search over strings confirms the minimum; counting the x letters alone ignores their positions.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic