-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00020-valid_parentheses.java
More file actions
36 lines (28 loc) · 894 Bytes
/
00020-valid_parentheses.java
File metadata and controls
36 lines (28 loc) · 894 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
36
// 20: Valid Parentheses
// https://leetcode.com/problems/valid-parentheses/
import java.util.Stack;
class Solution {
// SOLUTION
public boolean isValid (String s) {
Stack<Character> parentheses = new Stack<>();
for (var c : s.toCharArray()) {
switch(c) {
case '{': parentheses.push('}'); break;
case '[': parentheses.push(']'); break;
case '(': parentheses.push(')'); break;
default:
if (parentheses.isEmpty() || c!=parentheses.pop())
return false;
}
}
return parentheses.isEmpty();
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
String s = "()[]{}";
// OUTPUT
var result = o.isValid(s);
System.out.println(result);
}
}