Simple JavaScript Solution


  • 0
    var isValid = function(s) {
        var array = [];
        s = s.split( "" );
        for(let i = 0; i < s.length; i ++){
            if( s[i] === "(" ) array.push( ")" );
            else if( s[i] === "[" ) array.push( "]" );
            else if( s[i] === "{" ) array.push( "}" );
            else if( s.length === 0 || array[array.length - 1] !== s[i] ) return false;
            else array.pop();
        }
        return array.length === 0;
    };

Log in to reply
 

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