Skip to content

2116. Check if a Parentheses String Can Be Valid #1128

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can approach it step by step, keeping in mind the constraints and the behavior of the locked positions.

Key Points:

  1. If the length of the string is odd, we can immediately return false because a valid parentheses string must have an even length (each opening ( needs a closing )).
  2. We need to keep track of the number of open parentheses (() and closed parentheses ()) as we iterate through the string. If at any point the number of closing parentheses exceeds the number of opening ones, it's impossible to balance the string and we return false.
  3. We must carefully handle the positions that are locked (locked[i] == '1') and unlocked (locked[i] == '0'). Unlocked positions allow us to change th…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jan 12, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jan 12, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants