Digit DP

Counting numbers in huge ranges seems impossible. Digit DP builds answers digit by digit.

42 lessons
176 min
Codeforces: 1700-2100LeetCode: 1800-2200

Lessons

1. Intro

What you'll learn

4m

2. Core Concept

Digit-by-digit processing

5m

3. Vocabulary - Tight Bound

The key constraint

4m

4. Quiz: Tight Bound

Understanding the constraint

3m1 problems

5. Vocabulary - Leading Zeros

Handling shorter numbers

4m

6. Quiz: Leading Zeros

When zeros matter

3m1 problems

7. The General Pattern

State template

4m

8. Range Queries

From [L,R] to prefix

4m

9. LeetCode 902 Numbers At Most N Given Digit Set - Problem Statement

LeetCode 902

3m1 problems

10. Digit Set - The Setup

Understanding the constraints

4m

11. Digit Set - State Definition

What dp[pos][tight] means

4m

12. Digit Set - Transition

Picking valid digits

4m

13. Digit Set - Implementation

The code

7m1 problems

14. Digit Set - Walkthrough

Tracing the algorithm

4m

15. Lessons from Digit Set

summary

4m

16. Challenge: Digit Set Edge Cases

Handling tricky inputs

4m

17. LeetCode 2376 Count Special Integers - Problem Statement

LeetCode 2376

3m1 problems

18. Special Integers - The Setup

Tracking used digits

4m

19. Special Integers - State Definition

Adding the mask

4m

20. Special Integers - Transition

Checking the mask

5m

21. Special Integers - Implementation

The code

7m1 problems

22. Special Integers - Walkthrough

Tracing distinct digits

4m

23. Lessons from Special Integers

summary

4m

24. Quiz: Bitmask in Digit DP

Why bitmask works

3m1 problems

25. LeetCode 600 Non-negative Integers without Consecutive Ones - Problem Statement

LeetCode 600

3m1 problems

26. Consecutive Ones - The Setup

Working in binary

5m

27. Consecutive Ones - State Definition

Tracking previous bit

5m

28. Consecutive Ones - Transition

Avoiding 11

5m

29. Consecutive Ones - Implementation

The code

7m1 problems

30. Consecutive Ones - Walkthrough

Tracing with previous digit

4m

31. Lessons from Consecutive Ones

summary

4m

32. Challenge: K Consecutive Ones

Generalizing the constraint

4m

33. Pattern - Digit DP

Core idea

4m

34. Digit Sum - Problem Statement

Classic digit DP variant

3m

35. Digit Sum - Implementation

Memoization details

5m

36. Quiz: Digit DP Complexity

Analyzing state space

3m1 problems

37. Count Stepping Numbers - Problem Statement

Adjacent digit constraint

3m

38. Stepping Numbers - Implementation

Handling started flag

5m

39. Challenge: Digit DP on Multiple Numbers

Comparing two numbers

4m

40. Pattern - When to Use Digit DP

Recognizing the pattern

4m

41. What's Next

Preview of Game Theory DP

4m

42. Section Recap

What we learned

5m

Practice Problems

1.

Count numbers where digit sum is divisible by D - foundational.

2.

No adjacent same digits - teaches tight bound and leading zeros.

3.

Exactly K non-zero digits - handles N up to 100 digits.

4.
Classy NumbersCodeforcesmedium

At most 3 non-zero digits - practice count parameter in state.

5.

Restricted digit set - handling which digits can be used.

6.
Magic NumbersCodeforceshard

Positional constraints + divisibility - advanced digit DP.

7.

Covered with full walkthrough in this section.

8.

Covered with full walkthrough in this section.

9.
Magic NumbersCodeforcesmedium

Count numbers with d at even positions only, divisible by m. Classic digit DP.

10.
Classy NumbersCodeforcesmedium

Count numbers with at most 3 non-zero digits. Digit DP tracking non-zero count.

11.

Count numbers where all digits appear even times in base b. Multi-base digit DP.

12.

Count pairs where a XOR b = a + b. Two-variable digit DP on binary representation.

13.

Count numbers reaching 1 in exactly k steps via popcount. Digit DP + precomputation.

14.
Segment SumCodeforceshard

Sum of numbers using at most k distinct digits. Digit DP tracking digit set.

15.
Count of IntegersLeetCodehard

Count integers with digit sum in range. Standard digit DP with sum tracking.

16.

Adjacent digits differ by 1. Digit DP tracking last digit placed.

17.

Equal even/odd digits and divisible by k. Multi-constraint digit DP.

18.

Count numbers with at least one repeated digit. Complement counting with digit DP.

Ready to start learning?

Access all 42 lessons with interactive content and progress tracking.