1249. Minimum Remove to Make Valid Parentheses #261
-
Topics: Given a string s of Your task is to remove the minimum number of parentheses ( Formally, a parentheses string is valid if and only if:
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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. |
Beta Was this translation helpful? Give feedback.
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.