Strobogrammatic Number III - Problem

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Given two strings low and high that represent two integers where low <= high, return the number of strobogrammatic numbers in the range [low, high].

The digits that remain the same when rotated 180 degrees are: 0, 1, 6, 8, 9 where:

  • 0 → 0
  • 1 → 1
  • 6 → 9
  • 8 → 8
  • 9 → 6

Input & Output

Example 1 — Small Range
$ Input: low = "50", high = "100"
Output: 3
💡 Note: Strobogrammatic numbers in range [50,100] are: 69, 88, 96. Each looks the same when rotated 180°.
Example 2 — Single Digit Range
$ Input: low = "0", high = "9"
Output: 3
💡 Note: Single digit strobogrammatic numbers are: 0, 1, 8. Numbers 2,3,4,5,6,7,9 don't look the same when rotated.
Example 3 — No Valid Numbers
$ Input: low = "20", high = "30"
Output: 0
💡 Note: No strobogrammatic numbers exist between 20 and 30.

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 zero itself

Visualization

Tap to expand
Strobogrammatic Numbers in Range [50, 100]Range [50, 100]6988966→9, 9→68→8, 8→89→6, 6→9Valid digits: 0↔0, 1↔1, 6↔9, 8↔8, 9↔6Count: 3
Understanding the Visualization
1
Input Range
Given low="50" and high="100"
2
Check Numbers
Find numbers that look same when rotated 180°
3
Count Valid
Return count of strobogrammatic numbers in range
Key Takeaway
🎯 Key Insight: Generate valid strobogrammatic numbers recursively instead of checking every number in range
Asked in
Google 15 Facebook 12
28.0K Views
Medium Frequency
~35 min Avg. Time
847 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