-
Notifications
You must be signed in to change notification settings - Fork 2
/
RemoveInvalidParenthesis.java
105 lines (87 loc) · 2.71 KB
/
RemoveInvalidParenthesis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package Leetcode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author kalpak
*
* 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: [""]
*
*/
public class RemoveInvalidParenthesis {
public static List<String> removeInvalidParentheses(String s) {
Set<String> result = new HashSet<>();
int left = 0;
int right = 0;
int count = 0;
// Limit max removal left and right for backtracking boundary.
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
left++;
} else if (s.charAt(i) == ')') {
if (left > 0)
left--;
else
right++;
}
}
// Find how many min. no. of changes are required
count = left + right;
findValidParenthesisDFS(s, 0, result, new StringBuilder(""), count);
return new ArrayList<String>(result);
}
private static void findValidParenthesisDFS(String s, int index, Set<String> result, StringBuilder sb, int count) {
if (count < 0) {
return;
}
if (index == s.length()) {
if (count == 0) {
if (isValid(sb.toString())) {
result.add(sb.toString());
}
}
return;
}
char ch = s.charAt(index);
int len = sb.length();
if (ch == '(' || ch == ')') {
findValidParenthesisDFS(s, index + 1, result, sb, count - 1); // not use ')''('
findValidParenthesisDFS(s, index + 1, result, sb.append(ch), count); // use
} else {
findValidParenthesisDFS(s, index + 1, result, sb.append(ch), count); // just append it
}
sb.setLength(len); // backtrack
}
private static boolean isValid(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') count++;
if (c == ')' && count-- == 0)
return false;
}
return count == 0;
}
public static void main(String[] args) {
String str1 = "()())()";
System.out.println(removeInvalidParentheses(str1));
String str2 = "(a)())()";
System.out.println(removeInvalidParentheses(str2));
}
}