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?
- A. 2
- B. 3
- C. 0
- 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.
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
- From the string "aabbaabba" you may repeatedly delete any two ADJACENT characters that are different (one 'a' and one 'b'). What is the…
- For 5 2 7 1 4 6 3, how many split points i (0 ≤ i ≤ n−2) have XOR(A[0..i]) equal to XOR(A[i+1..n−1])?
- What is the length of the shortest contiguous block of 1 3 9 4 6 5 7 8 that, if sorted in place by itself, makes the WHOLE array sorted in…
- How many positions i in 0 1 2 2 3 1 4 satisfy: the value A[i] equals the number of elements BEFORE position i that are strictly smaller…
- In 6 0 8 2 4 5 1 9, a ramp is a pair i < j with A[i] ≤ A[j]. What is the largest possible width j − i over all ramps?
- A circular linked list holds the values 2 7 4 9 1 6 3 in order, with the last node linked back to the first. How many nodes are STRICTLY…