-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparentheses.java
More file actions
50 lines (48 loc) · 1.54 KB
/
parentheses.java
File metadata and controls
50 lines (48 loc) · 1.54 KB
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
public class Solution {
ArrayList<Character> left;
public boolean isValid(String s) {
// Start typing your Java solution below
// DO NOT write main() function
left=new ArrayList<Character>();
return valid(s);
}
public boolean valid(String s) {
if(s.equals(""))
if(left.isEmpty()) return true;
else return false;
char c=s.charAt(0);
if(c=='('||c=='{'||c=='[')
left.add(c);
else {
char l='[';
if(c==')') l='(';
else if(c=='}') l='{';
if(!left.contains(l)) return false; if(left.size()==0) return false;
if(left.get(left.size()-1)!=l) return false;
// the last element of the list must match current one
// use Stackk!
left.remove(left.size()-1); // remove(object) the first appearance
}
return valid(s.substring(1));
}
}
http://n00tc0d3r.blogspot.com/2013/04/longest-valid-parentheses.html
public class Solution {
ArrayList<String> r;
public ArrayList<String> generateParenthesis(int n) {
// Start typing your Java solution below
// DO NOT write main() function
r=new ArrayList<String>();
g("",n,n);
return r;
}
public void g(String sofar,int left,int right) {
if(left==0&&right==0) {
String cp=new String(sofar);
r.add(cp);
return;
}
if(left>0) g(sofar+"(",left-1,right);
if(left<right) g(sofar+")",left,right-1);
}
}