-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValid Parentheses using java
More file actions
41 lines (37 loc) · 1.06 KB
/
Copy pathValid Parentheses using java
File metadata and controls
41 lines (37 loc) · 1.06 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
class Solution {
public boolean isValid(String s) {
Stack<Character> st = new Stack<>();
if (s == null || s.length() == 0) return true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ')'){
if (!st.isEmpty() && st.peek() == '(') {
st.pop();
} else {
return false;
}
}
else if (s.charAt(i) == '}') {
if (!st.isEmpty() && st.peek() == '{') {
st.pop();
} else {
return false;
}
}
else if (s.charAt(i) == ']') {
if (!st.isEmpty() && st.peek() == '[') {
st.pop();
} else {
return false;
}
}
else {
st.push(s.charAt(i));
}
}
if (st.isEmpty()) {
return true;
} else {
return false;
}
}
}