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?

  1. A. 2
  2. B. 28
  3. C. 8
  4. 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.

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

Exams that ask this topic