Java solution


  • 0
    J
        public String countAndSay(int n) {
            String s = "1";
            
            for (int i = 0; i < n - 1; i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < s.length(); ) {
                    char c = s.charAt(j);
                    int cnt = 0;
                    while (j < s.length() && s.charAt(j) == c) {
                        cnt++;
                        j++;
                    }
                    sb.append(cnt).append(c);
                }
                s = sb.toString();
            }
            return s;
        }
    

Log in to reply
 

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