classSolution{ publicbooleanisValid(String s){ Map<Character,Character> map = new HashMap<>(); map.put('}','{'); map.put(']','['); map.put(')','('); char[] chars = s.toCharArray(); Stack<Character> stack = new Stack<>(); for(int i = 0; i < chars.length; i++){ char c = s.charAt(i);
// If the current character is a closing bracket. if (map.containsKey(c)) {
// Get the top element of the stack. If the stack is empty, set a dummy value of '#' char topElement = stack.empty() ? '#' : stack.pop();
// If the mapping for this bracket doesn't match the stack's top element, return false. if (topElement != map.get(c)) { returnfalse; } } else { // If it was an opening bracket, push to the stack. stack.push(c); }