Javascript solution using stack (Beats 90%)


  • 0
    R
    var isValid = function(s) {
        var stack = [],
            i;
        
        s = s.split("");
        
        for(i = 0; i < s.length; i++){
            switch(s[i]){
                case "(":
                    stack.unshift(")");
                    break;
                case "[":
                    stack.unshift("]");
                    break;
                case "{":
                    stack.unshift("}");
                    break;
                default:
                    if(stack.shift() !== s[i])
                        return false;
                    break;
            }
        }
        
        if(stack.length === 0)
            return true;
        return false;
    };

Log in to reply
 

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