Strobogrammatic Number III - Problem

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, 9 can appear in strobogrammatic numbers
  • When rotated 180°: 0→0, 1→1, 6→9, 8→8, 9→6
  • Numbers like 69, 88, 96, 1001, 6009 are strobogrammatic
  • Numbers like 25, 34, 78 are 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

example_1.py — Basic Range
$ Input: low = "50", high = "100"
Output: 3
💡 Note: The strobogrammatic numbers in range [50, 100] are: 69, 88, 96. Each of these numbers looks the same when rotated 180 degrees.
example_2.py — Single Digit Range
$ Input: low = "0", high = "0"
Output: 1
💡 Note: Only the number 0 is in the range [0, 0], and 0 is strobogrammatic since it looks the same upside down.
example_3.py — No Valid Numbers
$ Input: low = "10", high = "50"
Output: 0
💡 Note: There are no strobogrammatic numbers between 10 and 50. The next strobogrammatic number after single digits is 11, but it's not in this range.

Visualization

Tap to expand
Strobogrammatic Generation Tree"""0""1""8""101""111""181""609""619""689""808""818"Length 2: 11, 69, 88, 96(skip 00 - leading zero)Filter by Range [low, high]Count only numbers within boundsCompare by length first, then lexicographically💡 Key: Build from center outward with valid pairs: (0,0), (1,1), (6,9), (8,8), (9,6)
Understanding the Visualization
1
Foundation
Start with base cases: empty string and single digits 0, 1, 8
2
Recursive Growth
Wrap each smaller number with valid pairs: 00, 11, 69, 88, 96
3
Length Control
Generate numbers for each length from low.length to high.length
4
Range Filtering
Keep only numbers that fall within [low, high] bounds
Key Takeaway
🎯 Key Insight: Instead of checking every number, generate only valid strobogrammatic candidates using recursion, then filter by range - dramatically reducing time from O(range) to O(5^(n/2))

Time & Space Complexity

Time Complexity
⏱️
O(5^(n/2))

Where n is the maximum length. We generate 5^(n/2) strobogrammatic numbers

n
2n
Linear Growth
Space Complexity
O(5^(n/2))

Space to store all generated strobogrammatic numbers and recursion stack

n
2n
Linearithmic Space

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
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
12.6K Views
Medium Frequency
~25 min Avg. Time
420 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen