import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
public class LeetCode20 {
public static void main(String[] args) {
String str = "()(()))"; //true
System.out.println(judgeString(str));
}
public static boolean judgeString(String str) {
Stack<String> leftSymbolStack = new Stack<>();
Set leftSet = new HashSet();
leftSet.add("(");
leftSet.add("{");
leftSet.add("[");
Set rightSet = new HashSet();
rightSet.add(")");
rightSet.add("]");
rightSet.add("}");
String strArr[] = str.split("");
for (int i = 0; i < strArr.length; i++) {
if (leftSet.contains(strArr[i])) {
leftSymbolStack.push(strArr[i]);
} else {
if (leftSymbolStack.isEmpty()) {
return false;
}
String pushSym = strArr[i];
String peekSym = leftSymbolStack.peek();
if (pushSym.equals(")") && peekSym.equals("(")
|| pushSym.equals("}") && peekSym.equals("{")
|| pushSym.equals("]") && peekSym.equals("[")
) {
leftSymbolStack.pop();
} else {
return false;
}
}
}
if (!leftSymbolStack.isEmpty()) {
return false;
}
return true;
}
}