Data Structures & AlgorithmsHard

From the string "aabbaabba" you may repeatedly delete any two ADJACENT characters that are different (one 'a' and one 'b'). What is the minimum possible length of the string at the end?

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

Correct answer: A. 1

Every deletion removes one a and one b, so the final length is at least the difference of the counts; a search over all reachable strings shows the bound is attained here. The parity of the length alone is a weaker invariant.

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

Exams that ask this topic