Class Solution
-
- All Implemented Interfaces:
public final class Solution2116 - Check if a Parentheses String Can Be Valid\.
Medium
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(Aconcatenated withB), whereAandBare valid parentheses strings.It can be written as
(A), whereAis a valid parentheses string.
You are given a parentheses string
sand a stringlocked, both of lengthn.lockedis a binary string consisting only of'0's and'1's. For each indexioflocked,If
locked[i]is'1', you cannot changes[i].But if
locked[i]is'0', you can changes[i]to either'('or')'.
Return
trueif you can makesa valid parentheses string. Otherwise, returnfalse.Example 1:
Input: s = "))()))", locked = "010100"
Output: true
Explanation: locked1 == '1' and locked3 == '1', so we cannot change s1 or s3. We change s0 and s4 to '(' while leaving s2 and s5 unchanged to make s valid.
Example 2:
Input: s = "()()", locked = "0000"
Output: true
Explanation: We do not need to make any changes because s is already valid.
Example 3:
Input: s = ")", locked = "0"
Output: false
Explanation: locked permits us to change s0. Changing s0 to either '(' or ')' will not make s valid.
Constraints:
n == s.length == locked.length<code>1 <= n <= 10<sup>5</sup></code>
s[i]is either'('or')'.locked[i]is either'0'or'1'.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleancanBeValid(String s, String locked)-
-
Method Detail
-
canBeValid
final Boolean canBeValid(String s, String locked)
-
-
-
-