Count and Say
1 approach, code in every language
Problem description
The count-and-say sequence is built by describing the previous term out loud. The first term is the string "1". To create any later term, scan the previous term left to right, group consecutive identical digits together, and write down how many there are followed by the digit itself.
For example, "1" is spoken as "one 1" which becomes "11", and "11" is spoken as "two 1s" which becomes "21". Given a position n, return the n-th term of this sequence.
Example 1
Input: n = 1
Output: "1"
By definition the sequence starts with the string 1.
Example 2
Input: n = 4
Output: "1211"
The chain is 1 -> "11" -> "21" -> "1211", so the fourth term is 1211.
Constraints
1 <= n <= 30Each term is generated only from the term directly before it
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Run Report Lines is our own problem on the technique behind Count and Say: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free