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":

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

&nbsp;
Example 1:

Input: n = 1
Output: "1"
Explanation: This is the base case.


Example 2:

Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"


&nbsp;
Constraints:


	1 &lt;= n &lt;= 30


class Solution:
    def countAndSay(self, n: int) -> str:
        if n==1:
            return "1"
        
        x=self.countAndSay(n-1)
        count=1
        cur=x[0]
        res=""
        for i in range(1,len(x)):
            if cur==x[i]:
                count+=1
            else:
                res+=str(count)+str(cur)
                count=1
                cur=x[i]
                
        res+=str(count)+str(cur)
        return res
class Solution {
    public String countSay(int n, String[] mapper) {
        if (n == 1) return mapper[1];
        else {
            String say = "";
            if (mapper[n-1] != null) say += mapper[n-1];
            else say += countSay(n-1, mapper);
            String count = "";
            int cache = Integer.parseInt(say.substring(0, 1));
            int cntr = 1;
            if (say.length() < 2) {
                count += "1" + Integer.toString(cache);
            } else {
                for(int i=1;i<say.length();i++) {
                    if (cache == Integer.parseInt(say.substring(i, i+1))) {
                        cntr++;
                        if (i == say.length() - 1) {
                            count += Integer.toString(cntr) + Integer.toString(cache);
                            cntr = 1;
                        }
                    } else {
                        count += Integer.toString(cntr) + Integer.toString(cache);
                        if (i < say.length() - 1) {
                            cache = Integer.parseInt(say.substring(i, i+1));
                            cntr = 1;
                        } else if (i == say.length() - 1) {
                            count += "1" + say.substring(i, i+1);
                        }
                    }
                }
            }
            mapper[n] = count;
            return mapper[n];
        }
    }
    public String countAndSay(int n) {
        String mapper[] = new String[n+1];
        mapper[1] = "1";
        return countSay(n, mapper);
    }
}
class Solution {
public:
    string countAndSay(int n) {
        if(n == 1)
            return "1";
        else
        {
            string ans = "";
            string temp = countAndSay(n-1);
            int count = 1, curr = temp[0], i = 1;
            while(i < temp.length())
            {
                if(temp[i] == curr)
                    count++;
                else
                {
                    ans += to_string(count);
                    ans += curr;
                    count = 1;
                    curr = temp[i];
                }
                i++;
            }
            ans += to_string(count);
            ans += curr;

            return ans;
        }

    }
};
var countAndSay = function(n) {
    if(n==1) return '1';
    let index = 1, num = '1';
    while(index < n){
        num = say(num);
        index++;
    }
    return num;
};

function say(num) {
    let prev = num[num.length-1], count = 0, newNum = '';
    for(let index = num.length-1; index >= 0 ; index--) {
        if(prev == num[index]) {
            count++;
        }
        else{
            newNum = ''+count + prev + newNum;
            count = 1;
            prev = num[index];
        }
    }
    if(count) {
        newNum = ''+count + prev + newNum;
    }
    return newNum;
}
Count and Say