码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Invalid Parentheses 解答

时间:2015-11-07 06:38:09      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

Question

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())()"]
")(" -> [""]

Solution

看到parenthese的问题,第一反应是用栈。这题要求minimum number,所以想到用BFS遍历解空间树。

思路为:

层次依次为删除0个元素,1个元素,2个元素。。。

层次遍历所有的可能。如果有一种可能是valid,那么不再遍历下面的层。

 1 public class Solution {
 2     public List<String> removeInvalidParentheses(String s) {
 3         Set<String> visited = new HashSet<String>();
 4         List<String> result = new ArrayList<String>();
 5         List<String> current = new ArrayList<String>();
 6         List<String> next;
 7         current.add(s);
 8         boolean reached = false;
 9         // BFS
10         while (!current.isEmpty()) {
11             next = new ArrayList<String>();
12             for (String prev : current) {
13                 visited.add(prev);
14                 // If valid
15                 if (isValid(prev)) {
16                     reached = true;
17                     result.add(prev);
18                 }
19                 
20                 // If not reached, then delete
21                 if (!reached) {
22                     for (int i = 0; i < prev.length(); i++) {
23                         char tmp = prev.charAt(i);
24                         if (tmp != ‘(‘ && tmp != ‘)‘) {
25                             continue;
26                         }
27                         String newStr = prev.substring(0, i) + prev.substring(i + 1);
28                         if (!visited.contains(newStr)) {
29                             next.add(newStr);
30                             visited.add(newStr);
31                         }
32                     }
33                 }
34             }
35             if (reached) {
36                 break;
37             }
38             current = next;
39         }
40         return result;
41     }
42     
43     private boolean isValid(String s) {
44         Deque<Integer> stack = new LinkedList<Integer>();
45         for (int i = 0; i < s.length(); i++) {
46             char cur = s.charAt(i);
47             if (cur != ‘(‘ && cur != ‘)‘) {
48                 continue;
49             }
50             if (cur == ‘(‘) {
51                 stack.push(i);
52             }
53             if (cur == ‘)‘) {
54                 if (stack.isEmpty()) {
55                     return false;
56                 } else {
57                     stack.pop();
58                 }
59             }
60         }
61         return stack.isEmpty();
62     }
63 }

事实上,我们可以不用栈,用一个count来检测是否是valid parenthese.

 1 private boolean isValid(String s) {
 2     int count = 0;
 3     for (int i = 0; i < s.length(); i++) {
 4         char cur = s.charAt(i);
 5         if (cur != ‘(‘ && cur != ‘)‘) {
 6             continue;
 7         }
 8         if (cur == ‘(‘) {
 9             count++;
10         }
11         if (cur == ‘)‘) {
12             count--;
13             if (count < 0) {
14                 return false;
15             }
16         }
17     }
18     return count == 0;
19 }

 

Remove Invalid Parentheses 解答

标签:

原文地址:http://www.cnblogs.com/ireneyanglan/p/4944260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!