Bruteforce recursion in javascript


  • 0
    P
    var checkValidString = function(s) {
        return check(0, s, 0);
    };
    
    function check(count, s, idx) {
        if (count < 0) return false;
        if (idx >= s.length) return count === 0;
        
        switch (s[idx]) {
            case "(":
                return check(count + 1, s, idx + 1);
            case ")":
                return check(count - 1, s, idx + 1);
            case "*":
                return check(count + 1, s, idx + 1) || 
                    check(count - 1, s, idx + 1) || 
                    check(count, s, idx + 1);
        }
    };
    

Log in to reply
 

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