Imagine looking at a digital calculator display upside down - some numbers still look like valid numbers! A strobogrammatic number is a fascinating mathematical concept where a number appears identical when rotated 180 degrees.
Given two strings low and high representing integers where low ≤ high, your task is to count how many strobogrammatic numbers exist within the range [low, high].
Key Rules:
- Only digits
0, 1, 6, 8, 9can appear in strobogrammatic numbers - When rotated 180°:
0→0, 1→1, 6→9, 8→8, 9→6 - Numbers like
69, 88, 96, 1001, 6009are strobogrammatic - Numbers like
25, 34, 78are not (contain invalid digits)
This problem combines string manipulation, recursion, and clever mathematical insights to efficiently generate and count valid numbers within bounds.
Input & Output
Visualization
Time & Space Complexity
Where n is the maximum length. We generate 5^(n/2) strobogrammatic numbers
Space to store all generated strobogrammatic numbers and recursion stack
Constraints
- 1 ≤ low.length, high.length ≤ 15
- low and high consist of only digits
- low ≤ high
- low and high do not contain any leading zeros except for the zero itself