Simple JavaScript solution


  • 0
    R

    var isValid = function(s) {
    var iarr = [];
    iarr = s.split('');
    var stack = [];
    for (i=0;i<iarr.length;i++){
    if(iarr[i] === '(' || iarr[i] === '{' || iarr[i] === '['){
    stack.push(iarr[i]);
    } else if(iarr[i] === ')' && stack.length!=0 && stack.indexOf('(')!=-1){
    stack.pop();
    } else if(iarr[i] === ']' && stack.length!=0 && stack.indexOf('[')!=-1){
    stack.pop();
    } else if(iarr[i] === '}' && stack.length!=0 && stack.indexOf('{')!=-1){
    stack.pop();
    }else{
    console.log('false');
    return false;
    }
    }

    if (stack.length === 0){
    console.log('true');
    return true;
    }else{
    console.log('false');
    return false;
    }
    };


Log in to reply
 

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