Data Structures & AlgorithmsVery hard
A string of length 4 is drawn from the alphabet {(, ), *}. Each * may be read as '(', as ')', or as the empty string. How many such strings have at least one reading that is a valid bracket sequence?
- A. 2
- B. 28
- C. 8
- D. 29
Correct answer: B. 28
A wildcard that may be '(', ')' or empty keeps the set of achievable open counts an interval; a string is valid if that interval can reach 0 without going negative. Forbidding the empty reading, dropping the wildcards, or forcing every * to '(' each gives a different total.
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
- A linked list has nodes numbered 0 to 9 in order, and node 9's next pointer returns to node 3. Two pointers start at node 0: slow moves…
- In the sorted array [1, 1, 2, 2, 3, 4, 4, 5], how many different VALUE pairs {x, y} with x < y satisfy x + y = 6? (Repeated copies of a…
- Among all permutations of 1, 2, 3, 4 in which no two ADJACENT entries sum to 5, which one is lexicographically smallest? Give it as a…
- What is the smallest positive integer that is NOT the sum of any CONTIGUOUS (adjacent) block of [1, 2, 4, 8]?
- How many ways can [1, 2, 3, 4, 5, 7] be cut into one or more contiguous, non-empty segments so that EVERY segment has an even sum?
- How many contiguous subarrays of [1, 2, 1, 3, 2, 1, 4, 2, 3] contain each of the values 1, 2, ..., L exactly once, where L is the subarray…