Java Solution


  • 0
    L
    class Solution {
        public String countAndSay(int n) {
            if(n==1)
                return "1";
            else if(n==2)
                return "11";
            return generateNewString(countAndSay(n-1));
            
        }
        public String generateNewString(String s){
            String newString = "";
            char chBegin = s.charAt(0);
            int n = s.length();
            int count = 1;
            for(int i = 1;i<n;i++){
                if(s.charAt(i) == chBegin){
                    count++;
                 }
               else{
                    newString += String.valueOf(count)+chBegin;
                    chBegin = s.charAt(i);
                    count=1;}
                if(i==n-1)
                    newString += String.valueOf(count)+chBegin;
    
    }
             return newString;
    }
    }
    

Log in to reply
 

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