Easy Java solution, RT: 4ms


  • 0
    A
    public String countAndSay(int n)
     {  if(n==1)
        return "1";
        return = helper(n,"1");
     } 
    public String helper(int n , String x)
    {
        if(n==1)
        return x;
        int count =0; StringBuilder sb = new StringBuilder();
        for(int i=0;i<x.length();i++)
        {    count++;
            if(i+1==x.length()||x.charAt(i)!=x.charAt(i+1))
               {		
            	sb.append(count);
            	sb.append(x.charAt(i));
            	count =0;	
               }
         }
         n--;
         return helper(n,sb.toString());
     }

Log in to reply
 

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