[Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Example 1:

Input: "()())()"
Output: ["()()()", "(())()"]

Example 2:

Input: "(a)())()"
Output: ["(a)()()", "(a())()"]

Example 3:

Input: ")("
Output: [""]

删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。

说明: 输入可能包含了除 ( 和 ) 以外的字符。

示例 1:

输入: "()())()"
输出: ["()()()", "(())()"]

示例 2:

输入: "(a)())()"
输出: ["(a)()()", "(a())()"]

示例 3:

输入: ")("
输出: [""]

20ms
 1 class Solution {
 2     func removeInvalidParentheses(_ s: String) -> [String] {
 3         var paths = [String]()
 4         dfs(&paths, Array(s), 0, 0, ("(", ")"))
 5         return paths
 6     }
 7     
 8     fileprivate func dfs(_ paths: inout [String], _ s: [Character], _ lastValid: Int, _ lastRight: Int, _ parens: (Character, Character)) {
 9         var count = 0, s = s
10         
11         for i in lastValid ..< s.count {
12             if s[i] == parens.0 {
13                 count += 1
14             }
15             
16             if s[i] == parens.1 {
17                 count -= 1
18             }
19             
20             if count < 0 {
21                 for j in lastRight ... i {
22                     guard s[j] == parens.1 else {
23                         continue
24                     }
25                     
26                     guard j == 0 || s[j] != s[j - 1] || j == lastRight else {
27                         continue
28                     }
29                     
30                     dfs(&paths, Array(s[0 ..< j] + s[j + 1 ..< s.count]), i, j, parens)
31                 }
32                 return
33             }
34         }
35         
36         if parens.0 == "(" {
37             dfs(&paths, s.reversed(), 0, 0, (")", "("))
38         } else {
39             paths.append(String(s.reversed()))
40         }
41     }
42 }

40ms

 1 class Solution {
 2     func removeInvalidParentheses(_ s: String) -> [String] {
 3         
 4         var res = [String]()
 5         var repeated  = Set<String>()
 6         removeRight(s, &res, &repeated)
 7         return res
 8     }
 9     
10     func removeRight(_ s : String, _ res : inout [String], _ repeated : inout Set<String>)  {
11         let sArr = Array(s)
12         var count = 0
13         for i in 0..<sArr.count {
14             let c = sArr[i]
15             if c == "(" {
16                 count += 1
17             }else if c == ")" {
18                 count -= 1
19             }
20             
21             if count < 0 {
22                 for j in 0...i where sArr[j] == ")" {
23                     let r = String(sArr[0..<j] + sArr[j+1..<sArr.count])
24                     if repeated.contains(r) {
25                         continue
26                     }
27                     repeated.insert(r)
28                     removeRight(r, &res, &repeated)
29                 }
30                 return
31             }
32         }
33         if count == 0 {
34             res.append(s)
35             return
36         }
37         
38         removeLeft(s, &res, &repeated)
39     }
40     
41     func removeLeft(_ s : String, _ res : inout [String], _ repeated : inout Set<String>)  {
42         let sArr = Array(s)
43         var count = 0
44         for i in stride(from: sArr.count-1, to: -1, by: -1) {
45             let c = sArr[i]
46             if c == ")" {
47                 count += 1
48             }else if c == "("{
49                 count -= 1
50             }
51             
52             if count < 0 {
53                 for j in stride(from: sArr.count-1, to: i-1, by: -1) where sArr[j] == "(" {
54                     let r = String(sArr[0..<j] + sArr[j+1..<sArr.count])
55                     if repeated.contains(r) {
56                         continue
57                     }
58                     repeated.insert(r)
59                     removeLeft(r, &res, &repeated)
60                 }
61                 return
62             }
63         }
64         res.append(s)
65     }
66 }

84ms

  1 class Solution {
  2   func isValid(_ str:String) -> Bool {
  3     
  4     var left = 0
  5     
  6     for char in str {
  7       
  8       if char == Character("(") {
  9         
 10         left += 1
 11       }
 12       
 13       if char == Character(")") {
 14         
 15         left -= 1
 16       }
 17       
 18       if left < 0 {
 19         
 20         return false
 21       }
 22     }
 23     
 24     return left == 0
 25   }
 26   
 27   func removeInvalidParentheses(_ s: String) -> [String] {
 28     
 29     var results:[String] = []
 30     
 31     if isValid(s) {
 32       
 33       return [s]
 34     }
 35     
 36     let mustRemove = findInvalid(s)
 37     
 38     removeAndCheck(s, start:0, left: mustRemove.left, right: mustRemove.right, &results)
 39     
 40     return results
 41   }
 42   
 43   func removeAndCheck(_ str:String, start:Int, left:Int, right:Int, _ results:inout [String]) {
 44     
 45     if left == 0 && right == 0 {
 46       
 47       if isValid(str) {
 48         
 49         results.append(str)
 50       }
 51       
 52       return
 53     }
 54     
 55     for i in start..<str.count {
 56       
 57       let index = str.index(str.startIndex, offsetBy: i)
 58       
 59       let next = str.index(after: index)
 60       
 61       if i != start {
 62         
 63         let prev = str.index(before: index)
 64         
 65         if str[index] == str[prev] {
 66           
 67           continue
 68         }
 69       }
 70       
 71       let char = str[index]
 72       
 73       if char == Character("(") && left > 0 {
 74         
 75         let substr = String(str[str.startIndex..<index] + str[next...])
 76         
 77         removeAndCheck(substr, start:i, left: left - 1, right: right , &results)
 78       }
 79       
 80       if char == Character(")") && right > 0 {
 81         
 82         let substr = String(str[str.startIndex..<index] + str[next...])
 83         
 84         removeAndCheck(substr, start:i, left: left, right: right - 1, &results)
 85       }
 86     }
 87   }
 88   
 89   func findInvalid(_ str:String) -> (left:Int, right:Int) {
 90     
 91     var cnt1 = 0, cnt2 = 0
 92     
 93     for char in str {
 94       
 95       if char == Character("(") {
 96         
 97         cnt1 += 1
 98       }
 99       
100       if cnt1 == 0 {
101       
102         if char == Character(")") {
103           
104           cnt2 += 1
105         }
106       }
107       else {
108         
109         if char == Character(")") {
110           
111           cnt1 -= 1
112         }
113       }
114     }
115     
116     return (cnt1, cnt2)
117   }
118 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10241414.html