Count and Say Simple java solution...


  • 0
    S

    Here is my solution

    class Solution {
     
        public String countAndSay(int n) {
            if (n == 1)
                return "1";
            
            if (n == 2)
                return "11";
            
            StringBuilder str = new StringBuilder();
            String temp = countAndSay(n-1);
            int count = 1;
            int i = 1;
            for (i = 0; i < temp.length() - 1; i++) {
                if (temp.charAt(i) == temp.charAt(i+1))
                    count++;
                else {
                    str.append(count);
                    str.append(temp.charAt(i));
                    count = 1;
                }
            }
            str.append(count);
            str.append(temp.charAt(i));
            return str.toString();
        }
    }
    

Log in to reply
 

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