Javascript valid parenthesis


  • 0
    M
    var isValid = function(s) {
    var pairs={
        "\(" : "\)",
        "\{" : "\}",
        "\[" : "\]"
    }
    var open=[]
    for(var i = 0; i<s.length; i++){
        if(s[i] in pairs){
            open.push(s[i])
        }
        else {
            var current = open.pop()
            if(pairs[current] !== s[i]){
                return false
            }
        }
    }
    return open.length === 0
    

    };


Log in to reply
 

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