Design Gurus Logo
Count and Say (medium)

Problem Statement

Given an integer n greater than 0, return the n<sup>th</sup> count-and-say sequence.

A count-and-say sequence is a string of digits created using specific rules given below.

  • countAndSay(1) = "1"
  • countAndSay(2) = say countAndSay(1)
  • countAndSay(n) = say countAndSay(n - 1)

You start with "1", and for each subsequent string, you "read off" the digits of the previous string, counting the number of digits in groups of the same digit.

  • For instance, say "1223" is read off as one 1 + two 2 + one 3 or 112213.

Examples

  • Example 1:

    • Input: n = 3
    • Expected Output: "21"
    • Justification: The sequence is "1", "11" (one 1), "21" (two 1s).
  • Example 2:

    • Input: n = 4
    • Expected Output: "1211"
    • Justification: Continuing from the last example, "21" is read as "one 2 + one 1" or "1211".
  • Example 3:

    • Input: n = 5
    • Expected Output: "111221"
    • Justification: Continuing from the last example, "1211" is read as "one 1 + one 2 + two 1s" or "111221".

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory