Straightforward Java Solution


  • 28
    Z
     public class Solution {
        public String countAndSay(int n) {
            String s = "1";
            for(int i = 1; i < n; i++){
                s = countIdx(s);
            }
            return s;
        }
        
        public String countIdx(String s){
            StringBuilder sb = new StringBuilder();
            char c = s.charAt(0);
            int count = 1;
            for(int i = 1; i < s.length(); i++){
                if(s.charAt(i) == c){
                    count++;
                }
                else
                {
                    sb.append(count);
                    sb.append(c);
                    c = s.charAt(i);
                    count = 1;
                }
            }
            sb.append(count);
            sb.append(c);
            return sb.toString();
        }
    }

  • 0
    D

    Why dose my code look exactly same as yours but get TLE when n=14. I don't get it...

    public class Solution {
        public String countAndSay(int n) {
            String result ="1";
            for(int i = 1; i<n; i++){
                result = read(result);
            }
            return result;
        }
        public String read(String n){
            String ret =new String();
            char curr = n.charAt(0);
            int count = 1;
            for(int i = 1; i < n.length();i++){
                if(n.charAt(i) == curr){
                    count++;
                }else{
                    ret = ret + ((char)count+48) + curr;
                    count=1;
                    curr = n.charAt(i);
                }
            
            }
            ret = ret + ((char)count+48) + curr;
            return ret;
        }
    }

  • 0
    C

    That's why you should use StringBuilder in this case.


  • 0
    Z

    Why my program looks the same with yours except that yours have an countIdx function (mine put all the code into countAndSay body), but yours just use 2ms. My code uses 4ms.


  • 0
    A

    @zhibzhang What is the running time for this solution?


  • 0
    Y

    @acheiver I would say the running time of this solution is O(n ^ n), since we have to at least generate every solution from n = 1 to n = n, which is O(n) for each case.


  • 0
    L
     public String countAndSay(int n) {
            return count("1",n);
        }
        public String count(String s,int n){
            
            if(n==1){//the condition of end, get the sequence of the n times "count and say"
                return s;
            }
            int count=0;
            String str="";
            for(int i=0;i<s.length();i++){
                if(i+1<s.length()&&s.charAt(i)==s.charAt(i+1)){
                    count++;
                }else if(i+1<s.length()&&s.charAt(i)!=s.charAt(i+1)){
                    count++;
                    str+=count+""+s.charAt(i);
                    count=0;
                }else{//initalize the seed of string "1",it is just used once
                    count++;
                    str+=count+""+s.charAt(i);
                }    
                
            }
            
            return count(str,n-1);
        }
    

  • 0
    P

    @zhibzhang really straightforward.


Log in to reply
 

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