Digit DP Counter - Problem
Given a positive integer N, count how many numbers from 1 to N (inclusive) satisfy a specific digit property.
For this problem, we need to count numbers where no two adjacent digits are the same.
For example:
123is valid (all adjacent digits are different)112is invalid (digits 1 and 1 are adjacent and same)5is valid (single digit numbers are always valid)
Constraints:
1 ≤ N ≤ 10^18
Input & Output
Example 1 — Small Range
$
Input:
n = 25
›
Output:
22
💡 Note:
Valid numbers: 1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,23,24,25. Invalid: 11,22 (adjacent same digits). Total: 22 valid numbers.
Example 2 — Single Digit
$
Input:
n = 9
›
Output:
9
💡 Note:
All single digit numbers 1,2,3,4,5,6,7,8,9 are valid (no adjacent digits to compare). Count: 9
Example 3 — Larger Range
$
Input:
n = 100
›
Output:
90
💡 Note:
All single digits (1-9): 9 valid. Two-digit numbers: 10,12,13...98 excluding 11,22,33,44,55,66,77,88,99. Count: 9 + 81 = 90
Constraints
- 1 ≤ n ≤ 1018
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code