Problem: Count and Say

The count-and-say sequence is a sequence of digit strings defined by the recursive formula:

countAndSay(1) = "1" countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string. To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.

For example, the saying and conversion for digit string "3322251":

1.png

Given a positive integer n, return the nth term of the count-and-say sequence.

```class Solution {

public String countAndSay(int n) {
    String ans="1";
    for (int i = 1; i < n; i++) {
        String temp=ans;
        ans="";
        char pre=temp.charAt(0);
        int count=1;
        for (int j = 1; j < temp.length(); j++) {
            char ch= temp.charAt(j);
            if (ch==pre)count++;
            else {
                ans=ans+count+pre;
                pre=ch;
                count=1;
            }
        }
        ans=ans+count+pre;
    }
    return ans;
}
}

```

12.png

2.png