Data Structures & AlgorithmsHardCodeforces Div2 B · TCS NQT

Repeatedly delete any adjacent pair of letters that are 'a' and 'b' (in either order) from "abbaabbbaab" until none remains. What is the length of the final string?

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

Correct answer: D. 1

A stack reduces the string in one pass: push each letter, and pop when it is the other letter of the pair on top. Cancellations only happen between adjacent letters, so the stack finds every removal that is possible.

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

Exams that ask this topic