1249. Minimum Remove to Make Valid Parentheses¶
Description¶
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(Aconcatenated withB), whereAandBare valid strings, or - It can be written as
(A), whereAis 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 <= 105s[i]is either'(',')', or lowercase English letter.
Solution 1: Stack1 with character index¶
- We use a stack to store the index of the opening parenthesis
(froms. - When we encounter a closing parenthesis
), we pop the index from the stack if there's an open parenthesis to be paired. Otherwise, we will remove the closing parenthesis)from thes. - After that, we will iterate through stack and remove all the non-matching opening parenthesis
(from thes.
Time Complexity O(n)
-
Convert string
sto arraycharsto make it mutable- This loop iterates through each character in the input string
s, which hasncharacters. - Time complexity:
O(n), wherenis the length of the input strings.
- This loop iterates through each character in the input string
-
Using a stack
bracketsto track open brackets' indices- Pushing and popping elements from an array
bracketstakesO(1)time complexity on average. - In the worst case, each character could be an opening bracket, leading to
noperations of pushing or popping. - Time complexity:
O(n), wherenis the length of the input strings.
- Pushing and popping elements from an array
-
Iterating through the stack
bracketsto remove remaining open brackets- This loop iterates through the remaining indices in the
bracketsarray. - Time complexity:
O(m), wheremis the number of remaining open brackets.
- This loop iterates through the remaining indices in the
Overall, the time complexity of the function is O(n + m), where n is the length of the input string s, and m is the number of remaining open brackets in stack after balancing the parentheses.
Space Complexity O(n)
-
Create an array
charsto store the characters of the input strings.- The space required is proportional to the length of the input string
s. - Space complexity:
O(n), wherenis the length of the input strings.
- The space required is proportional to the length of the input string
-
Using a stack
bracketsto track open brackets' indices- The space required for the stack is proportional to the number of open brackets.
- In the worst case, all opening brackets are pushed onto the stack.
- Space complexity:
O(m), where m is the number of open brackets in the input strings.
-
Additional space for the
bracketsarray- This array stores indices of opening brackets temporarily until they are balanced.
- Space complexity:
O(m), wheremis the number of open brackets in the input strings.
Overall, the space complexity of the function is O(n + m), where n is the length of the input string s, and m is the number of open brackets in the input string s.
2Solution 2: Counting Open and Close Parentheses without Stack¶
- We can solve this problem without using a stack by counting the number of open and close parentheses.
- We will iterate through the input string
sand keep track of the number of open and close parentheses. - We will remove the extra closing parentheses
)by comparing the number of open and close parentheses. - After that, we will remove the extra open parentheses
(by iterating through the string in reverse order.
Time Complexity O(n)
Same as the previous solution, the time complexity is O(n).
Space Complexity O(n)
- Creating the string
ans- The space required for the
ansstring depends on the number of characters that need to be retained. - In the worst case, all characters in the input string are retained, so the space complexity is
O(n), wherenis the length of the input strings.
- The space required for the
- Additional variables (close, n, curopen, curclose, i)
- These variables are integers and do not depend on the size of the input string. They consume constant space.
- It has nothing do with size of input string
sas they have a fixed amount of memory. - Space complexity: O(1).
This approach involves iteratively scanning the input string from both ends and removing unmatched parentheses until all parentheses are balanced. Although both solutions have a space complexity of O(n), Solution 2 may perform better than Solution 1 due to its iterative removal of unmatched parentheses, potentially resulting in smaller intermediate input sizes.
Related Problems¶
-
Approach 2 in C++ by @akash2099 ↩