Data Structures & AlgorithmsHard

Why is the minimum number of adjacent swaps to make a string palindromic NOT simply half the number of mismatched mirror pairs?

  1. A. The mismatched pairs can always be fixed in one swap each.
  2. B. Palindromes never need swaps.
  3. C. Only the middle character matters.
  4. D. Moving one character past several neighbours costs one swap per position, so the cost depends on the exact positions of the letters.

Correct answer: D. Moving one character past several neighbours costs one swap per position, so the cost depends on the exact positions of the letters.

Each adjacent swap moves a character by one position, so bringing a letter across a distance costs that distance. The true cost is a positional matching problem, not a count of mismatches.

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

Exams that ask this topic