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)?
- A. 9
- B. 4
- C. 8
- 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.
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 digit string "53412", delete EXACTLY two characters (the remaining characters keep their order). What is the lexicographically…
- How many DISTINCT non-empty strings can be formed as subsequences (characters taken in order, not necessarily adjacent) of "abab"?
- Arrange the numbers 3 30 34 5 9 in some order and concatenate their decimal digits. What is the LARGEST number that can be produced?
- From 4 9 1 7 3 8 2, choose positions so that no two CHOSEN positions are adjacent in the array. What is the maximum possible sum of the…
- How many index pairs i < j in 2 7 4 9 6 3 8 have an ODD sum A[i] + A[j] while also satisfying A[i] < A[j]?
- In 3 1 4 1 5 9 2 6 5 3, what is the largest possible index distance j − i between two positions i < j that hold EQUAL values?