A feature flag stream is a string of the characters (, ), and *. Each * is a wildcard that may be treated as a single (, a single ), or an empty string. Decide whether there is some assignment of the wildcards that makes the whole string a valid balanced parenthesis string.
A string is a valid balanced parenthesis string when it is empty, or it is two valid strings concatenated, or it is ( followed by a valid string followed by ). Equivalently: reading left to right the number of ) never exceeds the number of (, and the totals are equal at the end.
A single line: the stream (it may be empty). It contains only the characters (, ), and *.
Print YES if some wildcard assignment yields a valid balanced string, otherwise NO.
(, ), and *.Example 1
Input
(*)
Expected
YES
Explanation
Treat the * as empty (or as a matching inner pair); the string becomes (), which is valid, so the answer is YES.
Example 2
Input
)(
Expected
NO
Explanation
The first character is ) with no ( before it, so no assignment can keep the running balance non-negative. The answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →