Description
Discussed in #261
Originally posted by mah-shamim August 9, 2024
Topics: String
, Stack
Given a string s of '('
, ')'
and lowercase English characters.
Your task is to remove the minimum number of parentheses ( '('
or ')'
, in any positions ) so that the resulting parentheses string is valid and return any valid string.
Formally, a parentheses string is valid if and only if:
- It is the empty string, contains only lowercase characters, or
- It can be written as
AB
(A
concatenated withB
), whereA
andB
are valid strings, or - It can be written as
(A)
, whereA
is a valid string.
Example 1:
- Input: s = "lee(t(c)o)de)"
- Output: "lee(t(c)o)de"
- Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
- Input: s = "a)b(c)d"
- Output: "ab(c)d"
Example 3:
- Input: s = "))(("
- Output: ""
- Explanation: An empty string is also valid.
Constraints:
1 <= s.length <= 105
s[i]
is either'('
,')'
, or lowercase English letter.
Hint:
- Each prefix of a balanced parentheses has a number of open parentheses greater or equal than closed parentheses, similar idea with each suffix.
- Check the array from left to right, remove characters that do not meet the property mentioned above, same idea in backward way.
Activity
mah-shamim commentedon Jan 11, 2025
The task is to remove the minimum number of parentheses from a given string to make it a valid parentheses string. A valid parentheses string is defined as:
AB
, whereA
andB
are valid parentheses strings.(A)
, whereA
is a valid parentheses string.Key Points
(
must have a corresponding closing parenthesis)
in a valid order.)
that don't have a matching opening parenthesis(
.(
that don't have a matching closing parenthesis)
.Approach
First Pass: Iterate over the string from left to right. Use a counter (
openCount
) to keep track of the balance between opening and closing parentheses.)
and the counteropenCount
is zero, it means there is no preceding(
to balance it, so skip this)
.(
, increase the counter, and for each valid)
, decrease the counter.Second Pass: After the first pass, iterate over the temporary stack in reverse. Use a counter to track the number of
)
characters.(
and the counter for)
is zero, it means there is no closing parenthesis to balance it, so skip this(
.Return the Result: The result list will be in reverse order, so reverse it back and return as a string.
Plan
Let's implement this solution in PHP: 1249. Minimum Remove to Make Valid Parentheses
Explanation:
The algorithm uses two passes to ensure that only valid parentheses are kept:
)
which don’t have matching opening parentheses(
.(
which don’t have matching closing parentheses)
.Each pass modifies the string based on the matching parentheses, making sure the final string is valid.
Example Walkthrough
Example 1:
Example 2:
Example 3:
Time Complexity
This approach ensures that we can efficiently remove invalid parentheses in a two-pass solution. By using a stack and maintaining a balance between opening and closing parentheses, we can guarantee a valid output string with minimal removals. The solution is both time-efficient and space-efficient, handling large input sizes within the problem's constraints.
#261, #262 leetcode problems 001249-minimum-remove-to-make-valid-pare…
#261, #262 leetcode problems 001249-minimum-remove-to-make-valid-pare…