JS solution


  • 0
    var isValid = function(s) {
        if (s.length % 2 === 1) return false;
        const left = ['(', '[', '{'],
              right = [')', ']', '}'],
              stack = [];
        for (let i = 0; i < s.length; i++) {
            if (left.indexOf(s[i]) !== -1) stack.push(s[i]);
            else if (left.indexOf(stack.pop()) !== right.indexOf(s[i])) return false;
        }
        return !stack.length;
    };
    

Log in to reply
 

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