Check if a Parentheses String Can Be Valid - Complete Solution Guide
Check if a Parentheses String Can Be Valid is LeetCode problem 2116, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 string locked , both of length n . locked is a binary string consisting only of '0' s and '1' s. For each index i of locked , If locked[i] is '1' , you
Detailed Explanation
The problem asks us to determine if a given parentheses string `s` can be made valid, given another string `locked` of the same length. If `locked[i]` is '1', then `s[i]` cannot be changed. If `locked[i]` is '0', then `s[i]` can be changed to either '(' or ')'. A valid parentheses string satisfies the following: it's either "()", or can be written as AB where A and B are valid, or can be written as (A) where A is valid.
Solution Approach
The solution uses a greedy approach with two passes. First, it checks if the length of the string is even. If not, it's impossible to make the string valid. Then, it iterates through the string from left to right, maintaining a `balance` counter. When `locked[i]` is '0' or `s[i]` is '(', we increment `balance`, representing a potential opening parenthesis. When `locked[i]` is '1' and `s[i]` is ')', we decrement `balance`, representing a closing parenthesis. If `balance` becomes negative, it means we have more closing parentheses than opening parentheses at some point, and it's impossible to balance the string up to that point, so we return false. After the first pass, we iterate from right to left, doing the same thing but treating ')' as an opening parenthesis and '(' as a closing parenthesis. This ensures that we can balance the string in both directions.
Step-by-Step Algorithm
- Step 1: Check if the length of the string `s` is even. If not, return `false` because an odd-length string cannot be a valid parentheses string.
- Step 2: Initialize a `balance` counter to 0.
- Step 3: Iterate through the string `s` from left to right (index `i` from 0 to n-1).
- Step 4: Inside the loop, check if `locked[i]` is '0' (meaning `s[i]` can be changed) or `s[i]` is '('. If either condition is true, increment `balance`.
- Step 5: Otherwise (if `locked[i]` is '1' and `s[i]` is ')'), decrement `balance`.
- Step 6: After each increment or decrement, check if `balance` is negative. If it is, return `false` because it means there are too many closing parentheses before encountering enough opening parentheses to balance them.
- Step 7: Reset the `balance` counter to 0.
- Step 8: Iterate through the string `s` from right to left (index `i` from n-1 to 0).
- Step 9: Inside the loop, check if `locked[i]` is '0' or `s[i]` is ')'. If either condition is true, increment `balance`.
- Step 10: Otherwise (if `locked[i]` is '1' and `s[i]` is '('), decrement `balance`.
- Step 11: After each increment or decrement, check if `balance` is negative. If it is, return `false` for the same reason as step 6.
- Step 12: If both loops complete without returning `false`, return `true` because the string can be made valid.
Key Insights
- Insight 1: A parentheses string can be valid only if its length is even.
- Insight 2: We can use a greedy approach to determine if a string can be valid by simulating the process of balancing parentheses from left to right and from right to left.
- Insight 3: The '0's in the `locked` string represent flexible positions that can be treated as either '(' or ')' to help balance the string.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Stack, Greedy.
Companies
Asked at: ServiceNow.