Skip to content

Commit be74398

Browse files
committed
solve valid parentheses
1 parent 51813d3 commit be74398

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

valid-parentheses/sora0319.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
Stack<Character> checking = new Stack<>();
4+
5+
for(char c : s.toCharArray()){
6+
if(c == '(' || c == '{' || c == '['){
7+
checking.push(c);
8+
continue;
9+
}
10+
if(checking.empty()) return false;
11+
12+
if(c == ')' && checking.peek() == '('){
13+
checking.pop();
14+
continue;
15+
}
16+
if(c == '}' && checking.peek() == '{'){
17+
checking.pop();
18+
continue;
19+
}
20+
if(c == ']' && checking.peek() == '['){
21+
checking.pop();
22+
continue;
23+
}
24+
return false;
25+
}
26+
if(!checking.empty()) return false;
27+
return true;
28+
}
29+
}
30+

0 commit comments

Comments
 (0)