Data Structures & AlgorithmsHard

Using only swaps of ADJACENT characters, what is the minimum number of swaps needed to turn the string "abcab" into a palindrome?

  1. A. 2
  2. B. 3
  3. C. 0
  4. D. 1

Correct answer: D. 1

Moving a character past its neighbours costs one swap per position, so the answer depends on the exact positions, not just the number of mismatched pairs. A breadth-first search over strings confirms the minimum for this small case.

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

Exams that ask this topic