Data Structures & AlgorithmsVery hard

When tracking a '*' wildcard that may be '(', ')' or empty while scanning brackets, why is it enough to keep a range [lo, hi] of possible open counts?

  1. A. The achievable open-bracket counts always form a contiguous range, so the two endpoints describe every possibility.
  2. B. Each wildcard always adds exactly one open bracket, so the count is fixed.
  3. C. Brackets never interact with wildcards, so a single counter suffices.
  4. D. The range is always [0, n], so it need not be tracked.

Correct answer: A. The achievable open-bracket counts always form a contiguous range, so the two endpoints describe every possibility.

Each character changes every possible count by a small amount, and the union of intervals stays an interval. Checking that lo is 0 at the end (and hi never falls below 0) decides validity.

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

Exams that ask this topic