删除无效的括号
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。
说明: 输入可能包含了除 ( 和 ) 以外的字符。
示例 1:
输入: "()())()"
输出: ["()()()", "(())()"]
示例 2:
输入: "(a)())()"
输出: ["(a)()()", "(a())()"]
示例 3:
输入: ")("
输出: [""]
Approach 1: Backtracking
Intuition
For this question, we are given an expression consisting of parentheses and there can be some misplaced or extra brackets in the expression that cause it to be invalid. An expression consisting of parentheses is considered valid only when every closing bracket has a corresponding opening bracket and vice versa.
This means if we start looking at each of the bracket from left to right, as soon as we encounter a closing bracket, there should be an unmatched opening bracket available to match it. Otherwise the expression would become invalid. The expression can also become invalid if the number of opening parentheses i.e. ( are more than the number of closing parentheses i.e. ).
Let us look at an invalid expression and all the possible valid expressions that can be formed from it by removing some of the brackets. There is no restriction on which parentheses we can remove. We simply have to make the expression valid.
The only condition is that we should be removing the minimum number of brackets to make an invalid expression, valid. If this condition was not present, we could potentially remove most of the brackets and come down to say 2 brackets in the end which form () and that would be a valid expression.
An important thing to observe in the above diagram is that there are multiple ways of reaching the same solution i.e. say the optimal number of parentheses to be removed to make the original expression valid is K. We can remove multiple different sets of K brackets that will eventually give us the same final expression. But, each valid expression should be recorded only once. We have to take care of this in our solution. Note that there are other possible ways of reaching one of the two valid expressions shown above. We have simply shown 3 ways each for the two valid expressions.
Coming back to our problem, the question that now arises is, how to decide which of the parentheses to remove?
Since we don't know which of the brackets can possibly be removed, we try out all the options!
For every bracket we have two choices:
- Either it can be considered a part of the final expression OR
- It can be ignored i.e. we can delete it from our final expression.
Such kind of problems where we have multiple options and we have no strategy or metric of deciding greedily which option to take, we try out all of the options and see which ones lead to an answer. These type of problems are perfect candidates for the programming paradigm, Recursion.
Algorithm
- Initialize an array that will store all of our valid expressions finally.
- Start with the leftmost bracket in the given sequence and proceed right in the recursion.
- The state of recursion is defined by the index which we are currently processing in the original expression. Let this index be represented by the character i. Also, we have two different variables left_count and right_count that represent the number of left and right parentheses we have added to our expression till now. These are the parentheses that were considered.
- If the current character i.e. S[i] (considering S is the expression string) is neither a closing or an opening parenthesis, then we simply add this character to our final solution string for the current recursion.
- However, if the current character is either of the two brackets i.e. S[i] == '(' or S[i] == ')', then we have two options. We can either discard this character by marking it an invalid character or we can consider this bracket to be a part of the final expression.
-
When all of the parentheses in the original expression have been processed, we simply check if the expression represented by expr i.e. the expression formed till now is valid one or not. The way we check if the final expression is valid or not is by looking at the values in left_count and right_count. For an expression to be valid left_count == right_count. If it is indeed valid, then it could be one of our possible solutions.
- Even though we have a valid expression, we also need to keep track of the number of removals we did to get this expression. This is done by another variable passed in recursion called rem_count.
- Once recursion finishes we check if the current value of rem_count is < the least number of steps we took to form a valid expression till now i.e. the global minima. If this is not the case, we don't record the new expression, else we record it.
One small optimization that we can do from an implementation perspective is introducing some sort of pruning in our algorithm. Right now we simply go till the very end i.e. process all of the parentheses and when we are done processing all of them, we check if the expression we have can be considered or not.
We have to wait till the very end to decide if the expression formed in recursion is a valid expression or not. Is there a way for us to cutoff from some of the recursion paths early on because they wouldn't lead to a solution? The answer to this is Yes! The optimization is based on the following idea.
For a left bracket encountered during recursion, if we decide to consider it, then it may or may not lead to an invalid final expression. It may lead to an invalid expression eventually if there are no matching closing bracket available afterwards. But, we don't know for sure if this will happen or not.
However, for a closing bracket, if we decide to keep it as a part of our final expression (remember for every bracket we have two options, either to keep it or to remove it and recurse further) and there is no corresponding opening bracket to match it in the expression till now, then it will definitely lead to an invalid expression no matter what we do afterwards.
e.g.
( ( ) ) )
In this case the third closing bracket will make the expression invalid. No matter what comes afterwards, this will give us an invalid expression and if such a thing happens, we shouldn't recurse further and simply prune the recursion tree.
That is why, in addition to having the index in the original string/expression which we are currently processing and the expression string formed till now, we also keep track of the number of left and right parentheses. Whenever we keep a left parenthesis in the expression, we increment its counter. For a right parenthesis, we check if right_count < left_count. If this is the case then only we consider that right parenthesis and recurse further. Otherwise we don't as we know it will make the expression invalid. This simple optimization saves a lot of runtime.
Now, let us look at the implementation for this algorithm.
1 import java.util.ArrayList; 2 import java.util.HashSet; 3 import java.util.List; 4 import java.util.Set; 5 6 class Solution { 7 8 private Set<String> validExpressions = new HashSet<String>(); 9 private int minimumRemoved; 10 11 private void reset() { 12 this.validExpressions.clear(); 13 this.minimumRemoved = Integer.MAX_VALUE; 14 } 15 16 private void recurse( 17 String s, 18 int index, 19 int leftCount, 20 int rightCount, 21 StringBuilder expression, 22 int removedCount) { 23 24 // If we have reached the end of string. 25 if (index == s.length()) { 26 27 // If the current expression is valid. 28 if (leftCount == rightCount) { 29 30 // If the current count of removed parentheses is <= the current minimum count 31 if (removedCount <= this.minimumRemoved) { 32 33 // Convert StringBuilder to a String. This is an expensive operation. 34 // So we only perform this when needed. 35 String possibleAnswer = expression.toString(); 36 37 // If the current count beats the overall minimum we have till now 38 if (removedCount < this.minimumRemoved) { 39 this.validExpressions.clear(); 40 this.minimumRemoved = removedCount; 41 } 42 this.validExpressions.add(possibleAnswer); 43 } 44 } 45 } else { 46 47 char currentCharacter = s.charAt(index); 48 int length = expression.length(); 49 50 // If the current character is neither an opening bracket nor a closing one, 51 // simply recurse further by adding it to the expression StringBuilder 52 if (currentCharacter != '(' && currentCharacter != ')') { 53 expression.append(currentCharacter); 54 this.recurse(s, index + 1, leftCount, rightCount, expression, removedCount); 55 expression.deleteCharAt(length); 56 } else { 57 58 // Recursion where we delete the current character and move forward 59 this.recurse(s, index + 1, leftCount, rightCount, expression, removedCount + 1); 60 expression.append(currentCharacter); 61 62 // If it's an opening parenthesis, consider it and recurse 63 if (currentCharacter == '(') { 64 this.recurse(s, index + 1, leftCount + 1, rightCount, expression, removedCount); 65 } else if (rightCount < leftCount) { 66 // For a closing parenthesis, only recurse if right < left 67 this.recurse(s, index + 1, leftCount, rightCount + 1, expression, removedCount); 68 } 69 70 // Undoing the append operation for other recursions. 71 expression.deleteCharAt(length); 72 } 73 } 74 } 75 76 public List<String> removeInvalidParentheses(String s) { 77 78 this.reset(); 79 this.recurse(s, 0, 0, 0, new StringBuilder(), 0); 80 return new ArrayList(this.validExpressions); 81 } 82 }