mediumStringArrays & Hashing

Count and Say

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

Recognize the pattern

describe previous term's digit groupsrun-length encodingiterative build

Brute force idea

Not applicable — iterative IS the only approach.

Better approach

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

Key invariant

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

Watch out for

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

Arrays & Hashing Pattern