-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParanthesis.java
More file actions
32 lines (27 loc) · 999 Bytes
/
Copy pathValidParanthesis.java
File metadata and controls
32 lines (27 loc) · 999 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
package LeetcodePractice;
import java.util.Map;
import java.util.Stack;
public class ValidParanthesis {
public static boolean isValid(String s) {
Stack<Character> storeStack = new Stack<>();
Map<Character, Character> map = Map.of(')', '(', '}', '{', ']', '[');
for (char ch : s.toCharArray()) {
if (map.containsKey(ch)) {
char topmostCharacter = storeStack.isEmpty() ? '#' : storeStack.pop();
if (topmostCharacter != map.get(ch)) {
return false;
}
} else {
storeStack.push(ch);
}
}
return storeStack.isEmpty();
}
public static void main(String[] args) {
System.out.println(isValid("()"));
System.out.println(isValid("()[]{}"));
System.out.println(isValid("(]"));
System.out.println(isValid("([)]"));
System.out.println(isValid("{[]}"));
}
}