My Accepted Java Solution


  • 0
    A
    public class Solution {
        public boolean isValid(String s) {
            Map<Character, Character > mapping = new HashMap<Character, Character>(){{
            put(')','(');put('}','{');put(']','[');}};
            
            Stack<Character> stack = new Stack<Character>();
            for(char c:s.toCharArray()){
                if(c==')'||c=='}'||c==']'){
                    if(stack.isEmpty()||stack.pop()!=mapping.get(c)){
                        return false;
                    }
                }else{
                    stack.push(c);
                }
            }
            return stack.isEmpty();
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.