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?
- A. The mismatched pairs can always be fixed in one swap each.
- B. Palindromes never need swaps.
- C. Only the middle character matters.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- When repeatedly deleting adjacent pairs of different letters, why does the final length have fixed parity?
- In the 'shortest block to sort so the whole array becomes sorted' problem, why does the block start at the first mismatch with the sorted…
- For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?
- For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?
- Why does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at…
- The minimum number of consecutive deletions to make an array non-decreasing is found by which structure?