-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.valid-parentheses.java
More file actions
36 lines (34 loc) · 964 Bytes
/
Copy path20.valid-parentheses.java
File metadata and controls
36 lines (34 loc) · 964 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
29
30
31
32
33
34
35
/*
* @lc app=leetcode id=20 lang=java
*
* [20] Valid Parentheses
*/
// @lc code=start
class Solution {
public boolean isValid(String s) {
Stack<Character> charSt = new Stack();
for (char eachChar : s.toCharArray()) {
switch (eachChar) {
case ')':
if (charSt.isEmpty() || charSt.pop() != '(') {
return false;
}
break;
case '}':
if (charSt.isEmpty() || charSt.pop() != '{') {
return false;
}
break;
case ']':
if (charSt.isEmpty() || charSt.pop() != '[') {
return false;
}
break;
default:
charSt.push(eachChar);
}
}
return charSt.isEmpty();
}
}
// @lc code=end