Count and say - JavaScript solution


  • 0
    Y
    var countAndSay = function(n) {
        let s = ['1'];
        let i = 0;
    
        while (i < n - 1) {
            for (let j = 0; j < s[i].length; j++) {
                let c = s[i][j];
                let k = 1;
    
                while (s[i][j + 1] === c) {
                    j++;
                    k++;
                }
    
                if (s[i + 1]) s[i + 1] += k + '' + c;
                else s[i + 1] = k + '' + c;
            }
    
            i++;
        }
    
        return s[n - 1];
    };
    

Log in to reply
 

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