Strobogrammatic Number - Problem

Imagine rotating a digital clock display 180 degrees - some numbers would still look like valid numbers, while others would become unreadable! This is the fascinating concept behind strobogrammatic numbers.

Given a string num representing an integer, determine if it's a strobogrammatic number - a number that looks identical when rotated 180 degrees (viewed upside down).

Key insight: Only certain digits remain valid when rotated:

  • 00
  • 11
  • 69
  • 88
  • 96

All other digits (2, 3, 4, 5, 7) become invalid when rotated.

Example: "69" is strobogrammatic because when rotated 180°, it becomes "69" again!

Input & Output

example_1.py — Basic Strobogrammatic
$ Input: num = "69"
Output: true
💡 Note: When '69' is rotated 180°: '6' becomes '9' and '9' becomes '6', resulting in '69' when read from right to left. Since it matches the original, it's strobogrammatic.
example_2.py — Invalid Digit
$ Input: num = "88"
Output: true
💡 Note: When '88' is rotated 180°: both '8's remain '8', so we get '88'. It matches the original string.
example_3.py — Non-Strobogrammatic
$ Input: num = "962"
Output: false
💡 Note: The digit '2' has no valid 180° rotation, so '962' cannot be strobogrammatic.

Visualization

Tap to expand
Strobogrammatic Number VisualizationDIGITAL DISPLAY6906180° RotationROTATED DISPLAY9069Original: 6906Rotated: 6906✓ MATCH!Valid Rotations:0→0, 1→1, 6→9, 8→8, 9→62,3,4,5,7 are invalid
Understanding the Visualization
1
Identify Valid Rotations
Only digits 0,1,6,8,9 can be rotated 180° and remain valid digits
2
Check Symmetry
For a number to be strobogrammatic, it must read the same when rotated
3
Pair Validation
Each digit position must match its rotated counterpart
Key Takeaway
🎯 Key Insight: A strobogrammatic number must be symmetric when rotated - use two pointers to check if corresponding digits are valid rotations of each other!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the string, checking at most n/2 pairs of digits

n
2n
Linear Growth
Space Complexity
O(1)

Only using constant extra space for pointers and rotation mapping

n
2n
Linear Space

Constraints

  • 1 ≤ num.length ≤ 50
  • num consists of only digits
  • num does not contain any leading zeros except for the number 0 itself
Asked in
Google 28 Amazon 15 Meta 12 Microsoft 8
28.4K Views
Medium Frequency
~15 min Avg. Time
982 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