-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesis.java
More file actions
31 lines (23 loc) · 808 Bytes
/
Parenthesis.java
File metadata and controls
31 lines (23 loc) · 808 Bytes
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
import java.util.ArrayList;
public class Parenthesis {
public boolean isValid(String s) {
// Start typing your Java solution below
// DO NOT write main() function
char[] sc = s.toCharArray();
if(sc.length<=1) return false;
ArrayList al = new ArrayList();
for(int i=0; i<sc.length; i++){
if(al.size()==0){
al.add(""+sc[i]);
}else if((sc[i]==')' &&al.get(al.size()-1).equals("("))||
(sc[i]==']' &&al.get(al.size()-1).equals("["))||
(sc[i]=='}' &&al.get(al.size()-1).equals("{"))
){
al.remove(al.size()-1);
}else {
al.add(""+sc[i]);
}
}
return al.size() == 0;
}
}