-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid-parentheses.java
More file actions
47 lines (42 loc) · 1.21 KB
/
valid-parentheses.java
File metadata and controls
47 lines (42 loc) · 1.21 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
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
int stop =0;
if(s == null && s.isEmpty())
return false;
for(int i = 0 ; i<s.length() && stop == 0 ; i++){
char ch = s.charAt(i);
if(ch == '(' || ch == '[' || ch == '{' ){
stack.push(ch);
}
else
{
if(!stack.isEmpty()){
char ch_top = stack.pop();
if(ch == ')' && ch_top != '('){
stop = 1;
return false;
}
else if(ch == ']' && ch_top != '['){
stop = 1;
return false;
}
else if(ch == '}' && ch_top != '{'){
stop = 1;
return false;
}
}
else{
return false;
}
}
}
if(!stack.isEmpty())
{
return false;
}
else{
return true;
}
}
}