Java Solution : 5ms


  • 0
    public class Solution 
    {
    
        public StringBuilder intToString(StringBuilder ans)
        {
            StringBuilder res = new StringBuilder();
            
            char temp='0'; 
            int counter = 0;
            int current = 0;
            
            for(int i=0 ; i<ans.length(); i++)
            {
                if(ans.charAt(i)!=temp)
                {
                    if(temp!='0')
                    {
                        res.append(counter);
                        res.append(current);
                    }
                    temp = ans.charAt(i);
                    counter = 1;
                    current = Character.getNumericValue(temp);
                }
                else
                    counter++;
            }
            
            res.append(counter);
            res.append(current);
           
            return res;
        }
        
        public String countAndSay(int n) 
        {
            StringBuilder ans = new StringBuilder("1");
            
            for(int i=1 ; i<n ; i++)
                ans = intToString(ans);
    
            return ans.toString();
        }
    }

Log in to reply
 

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