-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1249. Minimum Remove to Make Valid Parentheses #262
Comments
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:
Key Points
Approach
Plan
Let's implement this solution in PHP: 1249. Minimum Remove to Make Valid Parentheses <?php
/**
* @param String $s
* @return String
*/
function minRemoveToMakeValid(string $s): string
{
// Stack to keep track of characters that lead to a valid string
$stack = [];
// Counter to track the balance of parentheses
$openCount = 0;
// First pass to remove invalid closing parentheses
for ($i = 0; $i < strlen($s); $i++) {
$char = $s[$i];
// If a closing parenthesis is encountered with no matching open, skip it
if ($char == ')' && $openCount == 0) {
continue;
}
// If an opening parenthesis is found, increment the open count
if ($char == '(') {
$openCount++;
} // If a closing parenthesis is found and there is a matching open, decrement the open count
elseif ($char == ')') {
$openCount--;
}
// Add the character to the stack as it's part of valid substring so far
$stack[] = $char;
}
// Reset the open counter for the second pass
$openCount = 0;
// List to store the characters for the final answer
$answer = [];
// Second pass to remove invalid opening parentheses; process characters in reverse
for ($i = count($stack) - 1; $i >= 0; $i--) {
$char = $stack[$i];
// If an opening parenthesis is encountered with no matching close, skip it
if ($char == '(' && $openCount == 0) {
continue;
}
// If a closing parenthesis is found, increment the open count
if ($char == ')') {
$openCount++;
} // If an opening parenthesis is found and there is a matching close, decrement the open count
elseif ($char == '(') {
$openCount--;
}
// Add the character to the answer as it is part of a valid substring
$answer[] = $char;
}
// The characters in answer are in reverse order, reverse them back to the correct order
return implode('', array_reverse($answer));
}
// Example usage:
$s1 = "lee(t(c)o)de)";
$s2 = "a)b(c)d";
$s3 = "))((";
echo minRemoveToMakeValid($s1) . "\n"; // Output: "lee(t(c)o)de"
echo minRemoveToMakeValid($s2) . "\n"; // Output: "ab(c)d"
echo minRemoveToMakeValid($s3) . "\n"; // Output: ""
?> Explanation:The algorithm uses two passes to ensure that only valid parentheses are kept:
Each pass modifies the string based on the matching parentheses, making sure the final string is valid. Example WalkthroughExample 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. |
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:
AB
(A
concatenated withB
), whereA
andB
are valid strings, or(A)
, whereA
is a valid string.Example 1:
Example 2:
Example 3:
Constraints:
1 <= s.length <= 105
s[i]
is either'('
,')'
, or lowercase English letter.
Hint:
The text was updated successfully, but these errors were encountered: