Data Structures & AlgorithmsVery hardGATE CS · DSA

What is the length of the longest contiguous substring of ((()((()(()) that is a valid (balanced) parentheses string?

  1. A. 6
  2. B. 8
  3. C. 12
  4. D. 3

Correct answer: A. 6

Use a stack of indices seeded with −1: on ')' pop, and if the stack empties push the current index as a new base, else the length is i − top. The best is 6. Counting all matched pairs (8) ignores contiguity, 12 is the whole string and 3 is the number of pairs, not the length.

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

Exams that ask this topic