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 <= 30
  • Each term is generated only from the term directly before it

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