My naive Javascript solution


  • 0
    K
    var countAndSay = function(n) {
    if(n==1)return "1";
    var r="1";
    for(var i=1;i<n;i++){
        r=helper(r);
    }
    return r;
    };
    var helper=function(str){//1
    
    var count=1;
    var s="";
    for(i=0;i<str.length;i++)//11
    {
        if(i+1>=str.length)s=s+count+str[i];
        else if(i+1<str.length && (str[i]==str[i+1]))count++;
        else {
            s=s+count+str[i];
            count=1;
        }
        
        
      //  console.log(s);
       // if(s[i]!=s[i+1])count=1;
       
    }
    return s;
    

    };


Log in to reply
 

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