Given a sequence of brackets, validate if the sequence is valid.
public static boolean validate(String s){ char[] arr = s.toCharArray(); Stack<Character> stack = new Stack<>(); for(Character c:arr) { switch (c) { case '[': case '{': case '(': stack.push(c); break; case ']': case '}': case ')': { if(hash.values().contains(c)) { if(!stack.isEmpty() && hash.get(stack.peek())==c) stack.pop(); else if(stack.isEmpty()) return false; } } break; default: break; } } return stack.isEmpty(); }Read full article from Coding Recipies: Validate Brackets