Simple Java solution


  • 0
    J
    This post is deleted!

  • 0
    J
    class Solution {
        public String countAndSay(int n) {
            String s = "1";
            for (int i = 1; i < n; i++){
                String t = "";
                char c = s.charAt(0);
                int count = 0;
                for (int j = 0; j<s.length() ; j++){
                    if (s.charAt(j)==c){
                        count++;
                    } else {
                        t = t + String.valueOf(count) + c;
                        count = 1;
                        c = s.charAt(j);
                    }
                }
                s = t + String.valueOf(count) + c;
            }
            return s;
        }
    }
    

Log in to reply
 

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