Java Solution


  • 0

    '''

    class Solution {
    public String countAndSay(int n) {
    if (n == 1) {
    return "1";
    }

        String prev = "1";
        
        while (n != 1) {
            
            int i = 1;
            String cur = "";
            char curChar = prev.charAt(0);
            int count = 1;
            
            while (i < prev.length()) {
                if (prev.charAt(i) == curChar) {
                    count++;
                } else {
                    cur += Integer.toString(count) + curChar;
                    curChar = prev.charAt(i);
                    count = 1;
                }
                
                i++;
            }
            
            cur += Integer.toString(count) + curChar;
            prev = cur;
            n--;
        }
        
        return prev;
    }
    

    }

    '''


Log in to reply
 

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