-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1.java
More file actions
37 lines (31 loc) · 918 Bytes
/
Copy pathSolution1.java
File metadata and controls
37 lines (31 loc) · 918 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
37
package stack;
import java.util.HashMap;
import java.util.Stack;
class Solution1 {
HashMap<Character, Character> hmLambda = new HashMap<Character, Character>() { {
put('(',')');
put('[', ']');
put('{', '}');
}};
public boolean isValid(String s) {
if(s.length() % 2 != 0){
return false;
}
Stack<Character> stack = new Stack<Character>();
char[] charArray = s.toCharArray();
for(Character k : charArray){
if(hmLambda.containsKey(k)){
stack.push(k);
}else{
if(stack.isEmpty() || k != hmLambda.get(stack.pop())){
return false;
}
}
}
return true;
}
public static void main(String[] args) {
boolean valid = new Solution1().isValid("()");
System.out.println(valid);
}
}