[Leetcode] Remove Invalid parentheses remove illegal parentheses
Removes the minimum number of invalid parentheses, making the input string valid and returning all possible results.
Description: The input may contain (
)
characters other than and.
Example 1:
- 输入: "()())()"
- 输出: ["()()()", "(())()"]
Example 2:
- 输入: "(a)())()"
- 输出: ["(a)()()", "(a())()"]
Example 3:
- 输入: ")("
- 输出: [""]
The DFS solution first counts the number of extra parentheses, uses cnt1 to represent the extra opening parenthesis, and Cnt2 represents the extra closing parenthesis, because the left and right brackets of the given string are either the same, or the opening parenthesis are many, or the closing parenthesis is more, or the parentheses are more or fewer, such as "." So cnt1 and Cnt2 are either 0 or both are greater than 0, either one is 0 and the other is greater than 0. OK, the following into our recursive function, first of all, if when Cnt1 and Cnt2 are 0 o'clock, indicating that the number of parentheses around the same time, we call the IsValid sub-function to determine whether the correct, the correct words added in the result res and return. Otherwise, starting from start, the variable start here indicates where the current recursion begins, and we don't need to start from scratch every time, there will be a lot of repeated calculations. And for multiple identical parentheses together, we only delete the first one, such as "())", here are two right parenthesis, we do not have to delete the first or the second closing parenthesis will get "()", no difference, so only to calculate once on the line, we pass and the previous character comparison, if not the same, The description is the first closing parenthesis, and if the same is skipped directly. At this point if the cnt1 is greater than 0, indicating that there is more left parenthesis at this time, and if the current character is exactly the left parenthesis, we can delete the current opening parenthesis, continue to call recursion, the Cnt1 value should be reduced by 1, because a left parenthesis has been deleted. Similarly, if the Cnt2 is greater than 0, indicating that the right parenthesis at this time, and if the current character is exactly the right parenthesis, we can delete the current closing parenthesis, continue to call recursion, the Cnt2 value should be reduced by 1, because a closing parenthesis has been deleted. See the code below:
- class Solution (object):
- def removeinvalidparentheses (self, s):
- """
- : Type S:str
- : Rtype:list[str]
- """
- if len (s) = = 0:
- return [""]
- Count1 = 0
- Count2 = 0
- for Str_num in S:
- Count1 + = (str_num=="(")
- if count1 = = 0:
- Count2 + = (str_num==")")
- Else:
- Count1-= (str_num==")")
- Start = 0
- res = []
- Self. DFS (S, start, Count1, Count2, RES)
- return Res
- def DFS (self, S, start, Count1, Count2, res):
- if count1 = = 0 and Count2 = = 0: #一样多
- if self.isvalid (s):
- Res.append (s)
- Return
- Else:
- for I in range (start, Len (s)):
- if i! = start and s[i] = = S[i-1]:
- Continue #重复的只计算一次
- elif count1 > 0 and s[i] = = "(": #左多
- Self. DFS (s[:i]+s[i+1:], I, count1-1, Count2, RES)
- elif count2 > 0 and s[i] = = ")": #右多
- Self. DFS (s[:i]+s[i+1:], I, Count1, Count2-1, RES)
- def IsValid (self, string):
- Count = 0
- for I in range (len (string)):
- if string[i] = = "(":
- Count + = 1
- elif string[i] = = ")":
- Count-= 1
- if count < 0:
- return False
- return count = = 0
Read more
[Leetcode] Remove Invalid parentheses remove illegal parentheses