Strobogrammatic Number - Problem

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

Given a string num which represents an integer, return true if num is a strobogrammatic number.

When rotated 180 degrees, the digits transform as follows:

  • 00
  • 11
  • 69
  • 88
  • 96
  • All other digits (2, 3, 4, 5, 7) are invalid when rotated

Input & Output

Example 1 — Valid Strobogrammatic
$ Input: num = "69"
Output: true
💡 Note: When rotated 180°: '6' becomes '9' and '9' becomes '6', giving us '96'. Reversed: '69' equals original.
Example 2 — Invalid Digit
$ Input: num = "88"
Output: true
💡 Note: Both '8' digits remain '8' when rotated 180°, so '88' rotated is still '88'.
Example 3 — Contains Invalid Digit
$ Input: num = "962"
Output: false
💡 Note: The digit '2' cannot be rotated 180° to form a valid digit, so this is not strobogrammatic.

Constraints

  • 1 ≤ num.length ≤ 50
  • num consists of only digits.
  • num does not contain any leading zeros except for the zero itself.

Visualization

Tap to expand
Strobogrammatic Number INPUT String: num = "69" 6 index 0 9 index 1 Valid Rotations: 0-->0 1-->1 8-->8 6-->9 9-->6 Invalid Digits: 2, 3, 4, 5, 7 ALGORITHM STEPS 1 Two Pointers left=0, right=1 2 Compare Pairs Check if num[left] maps to num[right] when rotated Checking "69": 6 rotates to 9 OK 3 Move Pointers left++, right-- 4 Result All pairs valid --> true Any mismatch --> false FINAL RESULT Original: "69" 6 9 180 deg Rotated: "69" 9 6 Output: true "69" looks same when rotated 180 deg! Key Insight: Use two pointers from both ends. For each pair, check if the left digit maps to the right digit when rotated 180 degrees. Only 0, 1, 6, 8, 9 are valid. Time: O(n), Space: O(1). TutorialsPoint - Strobogrammatic Number | Optimal Two-Pointer Approach
Asked in
Google 45 Facebook 38 Microsoft 25
28.0K Views
Medium Frequency
~15 min Avg. Time
892 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