Solution in JavaScript:


  • 0
    M
    var isValid = function(s) {
        let arr = [];
        let openArr = ['(','{','['];
        let closeArr = [')','}',']'];
        var strArr = s.split('');
        for(i=0;i<strArr.length;i++){
            let val = strArr[i];
            if(openArr.includes(val)){
                    arr.push(val);          
            }else if(closeArr.includes(val)){
                let lastElem = arr.pop();
                let indexLastElem = openArr.indexOf(lastElem);
                 let indexCurrElem = closeArr.indexOf(val);
                    if(indexLastElem != indexCurrElem){
                        return false;
                    }
            }
        }
        if(arr.length == 0){
            return true;
        }else{
            return false;
        }
    };
    

Log in to reply
 

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