LeetCode #38 Medium

Count and Say

Term 1 is "1"; each next term reads the previous aloud ("1211" → one 1, one 2, two 1s → "111221"). Return term n.

stringsimulation
Open on LeetCode ↗
02

Intuition

Pure run-length encoding applied n−1 times: scan the current string, group equal consecutive digits, and emit count+digit for each run. There's no closed form — simulate.

How to spot this pattern

A simulation problem — there's no closed form, so the work is reading the spec exactly and running it n−1 times. The only real decision is how to group consecutive equal characters; a language's run-length grouping (groupby here) removes the manual counter and the off-by-one that comes with it.

03

Approach

1

Describe one string

Walk with a run pointer: count how many times the current char repeats, append str(count) + char, jump past the run.

2

Iterate n−1 times

Start from "1" and re-describe. Strings roughly grow ~30% per step (Conway's constant λ ≈ 1.304).

3

Groupby shortcut

itertools.groupby does the run detection declaratively — same complexity.

04

Solution & live demo

1from itertools import groupby
2 
3class Solution:
4 def countAndSay(self, n):
5 s = "1"
6 for _ in range(n - 1):
7 s = "".join(str(len(list(g))) + d for d, g in groupby(s))
8 return s
05

Common pitfalls

Looping n times instead of n − 1

✗ Wrong
for _ in range(n):
    s = ...
✓ Right
s = "1"
for _ in range(n - 1):
    s = ...

The sequence starts at "1" for n = 1, so that term is the seed, not a computed step. Running the loop n times returns the (n+1)-th term.

Emitting digit-then-count

✗ Wrong
s = "".join(d + str(len(list(g))) for d, g in groupby(s))
✓ Right
s = "".join(str(len(list(g))) + d for d, g in groupby(s))

The rule is "say how many, then say which" — "21" means one 2 followed by one 1, giving "1211". Reversing the pair produces a different sequence entirely.

Counting all occurrences rather than consecutive runs

✗ Wrong
from collections import Counter
counts = Counter(s)
✓ Right
for d, g in groupby(s):
    ...

Counter collapses non-adjacent duplicates: "1211" would report three 1s, but the run-length reading is one 1, one 2, two 1s. Only consecutive equal characters form a group.

06

Edge cases

n = 1

Loop runs zero times → "1".

Runs longer than 9

Can't happen — no three equal consecutive digits ever appear in the sequence, and counts stay single-digit.

07

Complexity

Time
O(λⁿ)
Space
O(λⁿ)
String length grows geometrically.