Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
619 B
6
Indexable
Never
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    let stack = [];
    for (elem of s) {
        if (["(", "{", "["].includes(elem)) {
            stack.push(elem);
        } else {
            var matchingOpener = stack.pop();
            if (
                !matchingOpener ||
                (elem == ")" && matchingOpener != "(") ||
                (elem == "}" && matchingOpener != "{") ||
                (elem == "]" && matchingOpener != "[")
            ) {
                return false
            }
        }
    }
    
    return stack.length === 0
};
Leave a Comment