mediumStringArrays & Hashing

Count and Say

mediumTime: O(2^n)Space: O(2^n)

Signals to notice

describe previous term's digit groupsrun-length encodingiterative build

Brute force first

Not applicable — iterative IS the only approach.

The key insight

Start from '1'. Each step: scan for consecutive same digits, output count + digit. O(n × length).

What must stay true

Each term is the run-length encoding of the previous term. The transformation is deterministic.

Easy way to go wrong

Forgetting the last digit group — output it after the loop ends.

Arrays & Hashing Pattern