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?
- A. The achievable open-bracket counts always form a contiguous range, so the two endpoints describe every possibility.
- B. Each wildcard always adds exactly one open bracket, so the count is fixed.
- C. Brackets never interact with wildcards, so a single counter suffices.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Two different strings produced equal rolling-hash values. What does this tell you?
- In iterative reversal of a singly linked list, which pointer must be saved BEFORE the current node's next pointer is changed?
- For sorted positive integers, the smallest value that cannot be formed as a subset sum equals R+1, where R is the reach. Why does a value…
- Items (weight, value) are [(5, 60), (4, 40), (6, 50), (3, 30), (2, 15), (7, 70)]. Each item can be taken at most once, and the total…
- A shop has these items as (weight, value, number available): [(2, 10, 2), (3, 14, 3), (5, 30, 1)]. You may take at most the listed number…
- How many subsets of [2, 3, 5, 7, 4, 1, 6, 8] (chosen by position, the empty set excluded) have a sum of exactly 10?