Problem 325
Valid Parentheses with Wildcards
Medium · 20 pts · String, Greedy, Dynamic Programming
Problem
A string contains the characters '(', ')' and '*'. Each '*' is a wildcard that can be treated as '(', as ')', or as nothing at all. Decide whether there is a way to choose the wildcards so that the result is a valid parentheses string: every '(' is closed by a later ')', and no ')' appears before its matching '('. An empty result counts as valid.
Input
A single string S of '(', ')' and '*'.
Output
Print YES if S can be made valid, otherwise NO.
Constraints
1 <= |S| <= 100000
Example input
(*)
Example output
YES
Explanation
In '(*)' the wildcard can simply be removed, leaving '()', which is valid.