/38. Count and Say

38. Count and Say

Medium
Strings62.4% acceptance

Given a positive integer sequence index k, generate the k-th term of the sequence defined as follows: The first term is "1". Each subsequent term is constructed by applying run-length encoding to the previous term, where each group of consecutive identical digits is replaced by the count followed by the digit. Return the k-th term as a string.

Example 1

Input: sequence_index=5

Output: "111221"

Explanation: count_and_say(1) = "1" count_and_say(2) = "11" count_and_say(3) = "21" count_and_say(4) = "1211" count_and_say(5) = "111221"

Example 2

Input: sequence_index=6

Output: "312211"

Explanation: count_and_say(6) = "312211"

Constraints

  • 1 <= sequence_index <= 30
Python (current runtime)

Case 1

Input: sequence_index=3

Expected: "21"

Case 2

Input: sequence_index=7

Expected: "13112221"

Case 3

Input: sequence_index=2

Expected: "11"