Skip to content

Practice Check if a Parentheses String Can Be Valid

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2116
LeetCode Problem

Check if a Parentheses String Can Be Valid

Problem Statement

For each position i of s, locked[i] = 1 fixes its parenthesis; locked[i] = 0 permits choosing either ( or ). Return whether the resulting string can be balanced, so every closing parenthesis matches an earlier opening one and none remain unmatched. Character positions and string length stay unchanged.

Source: Check if a Parentheses String Can Be Valid on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "))()))", locked = "010100"
Output
true

# Example 2

Input
s = "()()", locked = "0000"
Output
true

# Example 3

Input
s = ")", locked = "0"
Output
false

# Example 4

Input
s = "(((())(((())", locked = "111111010111"
Output
true

Constraints

n == s.length == locked.length
1 <= n <= 10^5
s[i] is either '(' or ')'.
locked[i] is either '0' or '1'.

Related LeetCode Problems

Practice more problems similar to Check if a Parentheses String Can Be Valid.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.