public String countAndSay(int n) {
        String str = "1";
        int num = 1;
        
        while(num < n) {
            num++;
            StringBuilder sb = new StringBuilder();
            int base = -1, count = 0, cur = 0;
            while(cur < str.length()) {
                if(base == -1) {
                    base = cur;
                    count = 1;
                } else if(str.charAt(base) == str.charAt(cur)) {
                    count++;
                } else {
                    sb.append(count);
                    sb.append(str.charAt(base));
                    base = cur;
                    count = 1;
                }
                cur++;
            }
            if(base != -1) {
                sb.append(count);
                sb.append(str.charAt(base));
            }
            str = sb.toString();
        }
        
        return str;
    }