Leetcode 301: 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 ).

Examples:

"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]

  1 public class Solution {
  2     public IList<string> RemoveInvalidParentheses(string s) {
  3         var result = new List<string>();
  4         
  5         var queue = new Queue<Tuple<string, int>>();
  6         var visited = new HashSet<string>();
  7         
  8         queue.Enqueue(new Tuple<string, int>(s, 0));
  9         
 10         while (queue.Count > 0)
 11         {
 12             var tuple = queue.Dequeue();
 13             var str = tuple.Item1;
 14             var start = tuple.Item2;
 15             
 16             if (IsValid(str))
 17             {
 18                 if (result.Count > 0 && str.Length < result[result.Count - 1].Length)
 19                 {
 20                     // this result and all the possible results in the queue are not the minimal cuts
 21                     return result;
 22                 }
 23                 
 24                 // we don't need to remove any more parentheses
 25                 result.Add(str);
 26             }
 27             else
 28             {
 29                 for (int i = start; i < str.Length; i++)
 30                 {
 31                     // https://leetcode.com/problems/remove-invalid-parentheses/discuss/75038/Evolve-from-intuitive-solution-to-optimal-a-review-of-all-solutions
 32                     if ((str[i] == '(' || str[i] == ')') && (i == start || str[i] != str[i - 1]))
 33                     {
 34                         var sub = str.Substring(0, i) + str.Substring(i + 1, str.Length - i - 1);
 35 
 36                         if (!visited.Contains(sub))
 37                         {
 38                             queue.Enqueue(new Tuple<string, int>(sub, i));
 39                             visited.Add(sub);
 40                         }
 41                     }
 42                 }
 43             }
 44         }
 45         
 46         return result;
 47     }
 48     
 49     private bool IsValid(string s)
 50     {
 51         int count = 0;
 52         
 53         for (int i = 0; i < s.Length; i++)
 54         {
 55             if (s[i] == '(')
 56             {
 57                 count++;
 58             }
 59             else if (s[i] == ')')
 60             {
 61                 count--;
 62                 
 63                 if (count < 0) return false;
 64             }
 65         }
 66         
 67         return count == 0;
 68     }
 69 }
 70 
 71 // not optimized
 72 public class Solution1 {
 73     public IList<string> RemoveInvalidParentheses(string s) {
 74         var result = new List<string>();
 75         
 76         var queue = new Queue<string>();
 77         var visited = new HashSet<string>();
 78         
 79         queue.Enqueue(s);
 80         
 81         while (queue.Count > 0)
 82         {
 83             var str = queue.Dequeue();
 84             
 85             if (IsValid(str))
 86             {
 87                 if (result.Count > 0 && str.Length < result[result.Count - 1].Length)
 88                 {
 89                     // this result and all the possible results in the queue are not the minimal cuts
 90                     return result;
 91                 }
 92                 
 93                 // we don't need to remove any more parentheses
 94                 result.Add(str);
 95             }
 96             else
 97             {
 98                 for (int i = 0; i < str.Length; i++)
 99                 {
100                     if (str[i] == '(' || str[i] == ')')
101                     {
102                         var sub = str.Substring(0, i) + str.Substring(i + 1, str.Length - i - 1);
103 
104                         if (!visited.Contains(sub))
105                         {
106                             queue.Enqueue(sub);
107                             visited.Add(sub);
108                         }
109                     }
110                 }
111             }
112         }
113         
114         return result;
115     }
116     
117     private bool IsValid(string s)
118     {
119         int count = 0;
120         
121         for (int i = 0; i < s.Length; i++)
122         {
123             if (s[i] == '(')
124             {
125                 count++;
126             }
127             else if (s[i] == ')')
128             {
129                 count--;
130                 
131                 if (count < 0) return false;
132             }
133         }
134         
135         return count == 0;
136     }
137 }

 



 1 public class Solution {
 2     public IList<string> RemoveInvalidParentheses(string s) {
 3         var result = new List<string>();
 4         
 5         var queue = new Queue<string>();
 6         var visited = new HashSet<string>();
 7         
 8         queue.Enqueue(s);
 9         
10         while (queue.Count > 0)
11         {
12             var str = queue.Dequeue();
13             
14             if (IsValid(str))
15             {
16                 if (result.Count > 0 && str.Length < result[result.Count - 1].Length)
17                 {
18                     // this result and all the possible results in the queue are not the minimal cuts
19                     return result;
20                 }
21                 
22                 // we don't need to remove any more parentheses
23                 result.Add(str);
24             }
25             else
26             {
27                 for (int i = 0; i < str.Length; i++)
28                 {
29                     if (str[i] == '(' || str[i] == ')')
30                     {
31                         var sub = str.Substring(0, i) + str.Substring(i + 1, str.Length - i - 1);
32 
33                         if (!visited.Contains(sub))
34                         {
35                             queue.Enqueue(sub);
36                             visited.Add(sub);
37                         }
38                     }
39                 }
40             }
41         }
42         
43         return result;
44     }
45     
46     private bool IsValid(string s)
47     {
48         int count = 0;
49         
50         for (int i = 0; i < s.Length; i++)
51         {
52             if (s[i] == '(')
53             {
54                 count++;
55             }
56             else if (s[i] == ')')
57             {
58                 count--;
59                 
60                 if (count < 0) return false;
61             }
62         }
63         
64         return count == 0;
65     }
66 }

 

posted @ 2018-01-26 08:18  逸朵  阅读(147)  评论(0)    收藏  举报