218. Count and say
The count-and-say sequence is a sequence of digit strings defined by the following recursive formula:
- countAndSay(1) = "1"
- For n > 1, countAndSay(n) is generated by describing countAndSay(n-1) in terms of the frequency and value of consecutive identical digits.
For example:
- "1" is read as "one 1" or "11".
- "11" is read as "two 1s" or "21".
- "21" is read as "one 2, then one 1" or "1211".
- "1211" is read as "one 1, one 2, then two 1s" or "111221".
- "111221" is read as "three 1s, two 2s, then one 1" or "312211".
Given a positive integer n, return the nth term of the count-and-say sequence.
Example 1:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) is described as "one 1" = "11"
countAndSay(3) is described as "two 1s" = "21"
countAndSay(4) is described as "one 2, then one 1" = "1211"
Example 2:
Input: n = 1
Output: "1"
Explanation:
This is the base case where countAndSay(1) is "1".
Now Your Turn!
Pick the correct output for the given inputInput: n = 5
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 30