AC clean Java solution


  • 0
    public String countAndSay(int n) {
        String s = "1";
        
        while (n-- > 1) {
            int c = 0;
            String t = "";
            
            for (int i = 0; i <= s.length(); i++) {
                if (i == s.length() || (i > 0 && s.charAt(i) != s.charAt(i - 1))) {
                    t += String.valueOf(c) + s.charAt(i - 1); c = 0; // say
                }
                c++; // count
            }
            
            s = t;
        }
        
        return s;
    }

Log in to reply
 

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