Why it matters

Digit DP handles many counting problems.

Advertisement

The architecture

State: position, tight (bound by N), leading zeros, aux.

Transition: try each digit.

Digit DP statePositioncurrent digit indexTight boundmatches prefix of NAux stateproblem-specificRecursion + memoization typical implementation
Digit DP structure.
Advertisement

How it works end to end

Range [L, R]: count(R) - count(L-1).

Applications: count numbers with digit sum k, digits all different, etc.