[ INFO ]category: Coding difficulty: medium freq: Optional first seen: 2026-03-13
[MEDIUM][CODING][OPTIONAL]
$catproblem.md
Practice/Meta/Leetcode 38. Count and Say
Leetcode 38. Count and Say
CodingOptional
Problem
You are given a positive integer n. Your task is to generate the nth term of a special sequence that follows these rules:
The first term is "1"
Each subsequent term is created by reading the previous term aloud, counting consecutive occurrences of each digit
For example:
Term 1: "1" (given)
Term 2: Read "1" as "one 1" → "11"
Term 3: Read "11" as "two 1s" → "21"
Term 4: Read "21" as "one 2, one 1" → "1211"
Term 5: Read "1211" as "one 1, one 2, two 1s" → "111221"
The transformation process involves scanning from left to right, grouping consecutive identical digits, and for each group, outputting the count followed by the digit itself.
Requirements
Return the nth term of the sequence as a string
Start counting from n = 1 (the first term is "1")
Process consecutive digit groups correctly
Handle the iterative nature of the sequence generation
Constraints
1 ≤ n ≤ 30
Each term will only contain digits from 1 to 3
The output string will not exceed reasonable length limits for the given constraints
Examples
Example 1:
Input: n = 1 Output: "1" Explanation: The first term is defined as "1".