-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.java
More file actions
56 lines (39 loc) · 1.47 KB
/
ValidParentheses.java
File metadata and controls
56 lines (39 loc) · 1.47 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
51
52
53
54
55
56
class ValidParentheses{
public boolean isValid(String s) {
int count = s.length();
if(count == 0){
return true;
}
boolean valid = true;
ArrayList<Character> paranthese = new ArrayList<Character>();
for(int i=0;i<count;i++){
if(s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '['){
paranthese.add(s.charAt(i));
} else if(paranthese.size()>0){
int lastIndex =paranthese.size() -1;
if(s.charAt(i) == ')' && paranthese.get(lastIndex) == '('){
paranthese.remove(lastIndex);
continue;
}
if(s.charAt(i) == '}' && paranthese.get(lastIndex) == '{'){
paranthese.remove(lastIndex);
continue;
}
if(s.charAt(i) == ']' && paranthese.get(lastIndex) == '['){
paranthese.remove(lastIndex);
continue;
}
valid = false;
break;
}else{
valid = false;
break;
}
}
if(paranthese.size()> 0){
return false;
} else{
return valid;
}
}
}