/** * @author rale * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. * **/ publicclassSolution1 { publicbooleanisValid(String s) { char[] list = s.toCharArray(); Stack<Character> stack = newStack<Character>(); for(char temp : list){ if(temp=='(' || temp=='[' || temp=='{'){ stack.push(temp); } if(temp=='}' && (stack.isEmpty()||stack.pop()!='{')){ returnfalse; } if(temp==')' && (stack.isEmpty()||stack.pop()!='(')){ returnfalse; } if(temp==']'&& (stack.isEmpty()||stack.pop()!='[')){ returnfalse; } } if(!stack.isEmpty()){ returnfalse; } returntrue; } }