Javascript recursion solution 98 ms beats 98% submissions


  • 0
    R
    /**
     * @param {number} n
     * @return {string}
     */
    var countAndSay = function(n) {
        if (n === 1) {
            return "1";
        }
        return count(countAndSay(n-1));
    };
    
    function count(strNum) {
        var say = "";
        var count = 1;
        for (var i=0; i < strNum.length; i++) {
            if (strNum[i+1] === strNum[i]) {
                count++;
            } else {
                say = say + count + "" + strNum[i];
                count = 1;
            }
        }
        return say;
    }
    

Log in to reply
 

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