Check if a Parentheses String Can Be Valid
specialist · 945 · lc medium +32 · verified · 45.1% accepted · 2,027 likes · top 28%
Description
A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid if any of the following conditions is true:
- It is ().
- It can be written as AB (A concatenated with B), where A and B are valid parentheses strings.
- It can be written as (A), where A is a valid parentheses string.
You are given a parentheses string s and a binary string locked of the same length n. For each index i:
- If locked[i] is '1', the character s[i] is fixed and cannot be changed.
- If locked[i] is '0', you may change s[i] to either '(' or ')'.
Return true if you can make s a valid parentheses string, otherwise return false.
Example 1:
Example 2:
Example 3:
Example 4:
Code