Data Structures & AlgorithmsVery hardCodeforces Div2 C · TCS NQT

A string of 8 brackets is "]][[][][" where '[' and ']' appear equally often. Each operation swaps any two characters. What is the minimum number of swaps needed to make the string a balanced bracket sequence?

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

Correct answer: A. 1

Count the unmatched closing brackets with a running balance: the number of swaps is ⌈unmatched/2⌉ after the greedy match. Counting the total number of misplaced brackets over-counts, since one swap can fix two of them.

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

Exams that ask this topic