Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming

Digit DP

O(digits · states)

Build the number digit by digit with a 'tight' flag tracking whether you're still bounded by the limit.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn first1D / Fibonacci-styleMath / Number Theory
0%
mastery
0/10 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Count integers in [L, R] (N up to 1e18) whose digits satisfy a constraint; answer = f(R) - f(L-1).

  1. 1Brute force

    Iterate every number up to N.

  2. 2The observation

    Iterating N numbers: numbers sharing a prefix and state behave identically.

  3. 3Why it works

    Once a prefix is strictly below N's prefix, the remaining digits are free and states can be memoised.

Reach for it when

  • +N up to 1e18
  • +answer = f(R) - f(L-1)

Not when

  • ×Property depends on the whole number non-locally, not digit by digituse Math
  • ×A closed-form combinatorial count existsuse Combinatorics

Practice

Next · Foundation

Count Numbers with Unique Digits

Pure combinatorics; digit DP not needed yet.

0/10 solved
  • Count Numbers with Unique DigitsFoundationFoundationMedium
  • Numbers At Most N Given Digit SetApply it · Upper bound N, digit set.Apply itHard
  • Counting NumbersOne change · Range [a, b] with adjacent-digit constraint.One changeCSESMedium
  • Number of Digit OneTwists · Count digits, not numbers.TwistsHard
  • Numbers With Repeated DigitsBranches · Complement counting with a used-digit mask.BranchesHard
  • Count of IntegersAdvanced · Two string bounds and a digit-sum window.AdvancedHard
  • Magic NumbersMastery checkMastery checkCodeforces2200
  • Writing NumbersCSESHard
  • Removing Digits IICSESHard
  • Count Special IntegersHard

Pitfalls

Classic bugs

  • !Memoising tight states: only non-tight states are reusable; skip the cache (or key on tight) when tight.
  • !Leading zeros can change the property (digit counts, 'no repeated digits'); track a started flag.
Go deeper1 combinations · resources

Combinations

  • Digit DP+Bitmask DP

    count numbers <= N with all distinct (or repeated) digits

    Digit DP:
    the tight-prefix walk over the digits of N
    Bitmask DP:
    mask of digits already used

    Count Special IntegersNumbers With Repeated Digits

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
Bitmask DPMonotonic Stack
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial