Straightforward Java Solution


  • 0
    T
    public class Solution{
        public String countAndSay(int n){
            String s = "1";
            for (int i=1;i<n;i++){
                s = helperMethod(s);
            }
            return s;
        }
        public String helperMethod (String st) {
            StringBuilder sb = new StringBuilder();
            int len = st.length();
            int i=0;
            int count;
            while (i<len) {
                count =1;
                while (i<len-1 && st.charAt(i)==st.charAt(i+1)) {
                    i++;
                    count++;
                }
                sb.append(count).append(st.charAt(i));
                i++;
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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