-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoveInvalidParentheses.java
90 lines (88 loc) · 2.9 KB
/
RemoveInvalidParentheses.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
/*https://leetcode.com/problems/remove-invalid-parentheses/*/
class Solution {
Set<String> validExps;
int minRemoved;
public List<String> removeInvalidParentheses(String s) {
int left = 0, right = 0, n = s.length();
minRemoved = Integer.MAX_VALUE;
validExps = new HashSet<String>();
recur(s,0,0,0,0,new StringBuilder());
return new ArrayList<String>(validExps);
}
private void recur(String s, int index, int lc, int rc, int removed,StringBuilder exp)
{
if (index == s.length())
{
if (lc == rc && removed < minRemoved)
{
validExps = new HashSet<String>();
validExps.add(exp.toString());
minRemoved = removed;
}
else if (lc == rc && removed == minRemoved)
validExps.add(exp.toString());
}
else
{
char ch = s.charAt(index);
int len = exp.length();
if (ch != '(' && ch != ')')
{
exp.append(ch);
recur(s,index+1,lc,rc,removed,exp);
exp.deleteCharAt(len);
}
else
{
recur(s,index+1,lc,rc,removed+1,exp);
exp.append(ch);
if (ch=='(')
recur(s,index+1,lc+1,rc,removed,exp);
else if (rc < lc)
recur(s,index+1,lc,rc+1,removed,exp);
exp.deleteCharAt(len);
}
}
}
}
class Solution {
Set<String> validExps;
public List<String> removeInvalidParentheses(String s) {
int left = 0, right = 0, n = s.length();
validExps = new HashSet<String>();
for (int i = 0; i < n; ++i)
{
if (s.charAt(i) == '(') ++left;
else if (s.charAt(i) == ')')
{
if (left == 0) ++right;
if (left > 0) left = left-1;
}
}
recur(s,0,0,0,left,right,new StringBuilder());
return new ArrayList<String>(validExps);
}
private void recur(String s, int index, int lc, int rc, int left, int right, StringBuilder exp)
{
if (index == s.length())
{
if (left == 0 && right == 0)
validExps.add(exp.toString());
}
else
{
char ch = s.charAt(index);
int len = exp.length();
if ((ch == '(' && left > 0) || (ch == ')' && right > 0))
recur(s,index+1,lc,rc,left-(ch == '(' ? 1 : 0),right-(ch == ')' ? 1 : 0),exp);
exp.append(ch);
if (ch != '(' && ch != ')')
recur(s,index+1,lc,rc,left,right,exp);
else if (ch == '(')
recur(s,index+1,lc+1,rc,left,right,exp);
else if (rc < lc)
recur(s,index+1,lc,rc+1,left,right,exp);
exp.deleteCharAt(len);
}
}
}