-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe20.java
39 lines (36 loc) · 1.29 KB
/
e20.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
class Solution {
public boolean isValid(String s) {
Stack<Character> temp = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '[' || s.charAt(i) == '(' || s.charAt(i) == '{') {
temp.add(s.charAt(i));
} else if (s.charAt(i) == ']' || s.charAt(i) == ')' || s.charAt(i) == '}') {
if (temp.isEmpty()) {
return false;
} else {
switch (s.charAt(i)) {
case ']' :
if (temp.peek() != '[') {
return false;
}
break;
case '}' :
if (temp.peek() != '{') {
return false;
}
break;
case ')' :
if (temp.peek() != '(') {
return false;
}
break;
default :
return false;
}
temp.pop();
}
}
}
return temp.isEmpty();
}
}