My Javascript 116ms solution


  • 2
    S

    var isValid = function(s) {

    var valid = true,
        pair = {
            '(': ')',
            '[': ']',
            '{': '}'
        },
        nowWaitingFor = [];
    
    for (var i = 0; i < s.length; i++) {
        var nowChar = s.charAt(i);
    
    
        if (nowChar.match(/[(\[\{]/)) {
            
            nowWaitingFor.unshift(pair[nowChar]);
            
        } else if (nowChar.match(/[)\]\}]/)) {
            
            if (nowWaitingFor[0] !== nowChar) {
                valid = false;
                break;
            } else {
                nowWaitingFor.splice(0, 1);
            }
        }
    }
    
    if (nowWaitingFor.length !== 0) {
        valid = false;
    }
    
    return valid;
    

    };


Log in to reply
 

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