-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (25 loc) · 796 Bytes
/
Solution.java
File metadata and controls
28 lines (25 loc) · 796 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
package validParentheses;
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '{' || c == '[' || c == '(') {
stack.push(c);
} else {
if (stack.isEmpty()) {
return false;
}
char cOut = stack.pop();
boolean b1 = c == ')' && cOut != '(';
boolean b2 = c == ']' && cOut != '[';
boolean b3 = c == '}' && cOut != '{';
if (b1 || b2 || b3) {
return false;
}
}
}
return stack.isEmpty();
}
}