-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution20.java
More file actions
30 lines (26 loc) · 799 Bytes
/
Copy pathSolution20.java
File metadata and controls
30 lines (26 loc) · 799 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
package com.leetode;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class Solution20 {
public static boolean isValid(String s) {
Stack stack = new Stack();
Map<Character,Character> maps = new HashMap();
maps.put(')', '(');
maps.put(']', '[');
maps.put('}', '{');
char[] sarr = s.toCharArray();
for(int i = 0; i < sarr.length; i++){
if(!maps.containsKey(sarr[i])){
stack.push(sarr[i]);
}else if(stack.empty() || maps.get(sarr[i]) != stack.pop()){
return false;
}
}
return stack.empty();
}
public static void main(String[] args) {
String s1 = "(){{}}[(]";
System.out.println(isValid(s1));
}
}