/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 the string "1". Each subsequent term is produced by applying run-length encoding to the previous term, where each group of consecutive identical digits is replaced by the count of digits followed by the digit itself. Return the k-th term as a string.

Example 1

Input: sequence_index=5

Output: 111221

Explanation: Term 1: "1" Term 2: "11" Term 3: "21" Term 4: "1211" Term 5: "111221"

Example 2

Input: sequence_index=3

Output: 21

Explanation: Term 1: "1" Term 2: "11" Term 3: "21"

Constraints

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

Case 1

Input: sequence_index=6

Expected: 312211

Case 2

Input: sequence_index=2

Expected: 11

Case 3

Input: sequence_index=7

Expected: 13112221

Case 4

Input: sequence_index=8

Expected: 1113213211