My Java Solution


  • 0
    E
    public class Solution {
        public String countAndSay(int n) {
            if(n==0){
                return 0+"";
            }
            String res="1";
            int i=0;
            while(n>1){
                i=0;
                char ch=res.charAt(i);
                int val=0;
                String x="";
                while(i<res.length()){
                   if(ch==res.charAt(i)){
                       val++;
                   } 
                   else{
                       x=x+val+""+ch;
                       val=0;
                       if(i==res.length()-1){
                    	   x=x+"1"+res.charAt(i);
                       }
                       else{
                    	   val++;
                       }
                   }
                   ch=res.charAt(i);
                   i++;
                }
                if(val!=0){
                    x=x+val+""+ch;
                }
                res=x;
                n--;
            }
            return res;
        }
    }
    

Log in to reply
 

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