Skip to main content

Strobogrammatic Number - Solution & Explanation

EasyPremiumFree on FleetCodeHash TableTwo PointersString6 min readAsked at: Meta, Google
Practice this problem

Problem Statement

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

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

 

Example 1:

Input: num = "69"
Output: true

Example 2:

Input: num = "88"
Output: true

Example 3:

Input: num = "962"
Output: false

 

Constraints:

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

Approach Overview

Problem Overview: A number is strobogrammatic if it appears the same when rotated 180 degrees. Digits like 0, 1, and 8 remain the same after rotation, while 6 becomes 9 and 9 becomes 6. Given a numeric string, verify whether the rotated version still forms the same number.

Approach 1: Build Rotated String with Mapping (O(n) time, O(n) space)

Create a rotation map for valid digit pairs: {0→0, 1→1, 6→9, 8→8, 9→6}. Iterate through the string from right to left, append the rotated counterpart for each digit, and build a new string. If a digit is not present in the mapping, the number cannot be strobogrammatic. After constructing the rotated string, compare it with the original. This approach is straightforward and clearly models the rotation process, but it uses extra memory to store the reversed representation.

The idea relies on constant-time lookups using a hash table. Each digit is validated and converted using the mapping. If the final constructed string equals the original input, the number remains unchanged after a 180° rotation. Time complexity is O(n) because every digit is processed once, while space complexity is O(n) for the auxiliary string.

Approach 2: Two Pointers Simulation (O(n) time, O(1) space)

Use two pointers starting at the leftmost and rightmost digits. For every step, verify that the left digit correctly rotates to the right digit using the same valid mapping. For example, if the left pointer is at 6, the right pointer must be 9. Move both pointers inward until they cross.

This works because a strobogrammatic number must be symmetric under rotation. Instead of constructing a new string, you validate each mirrored pair directly. A middle digit (when length is odd) must be one of 0, 1, or 8 since these rotate to themselves.

The two-pointer scan processes each pair once, giving O(n) time complexity. It stores only the mapping and pointer indices, so the space complexity is O(1). The pattern is a common application of two pointers combined with digit validation on a string.

Recommended for interviews: Interviewers usually expect the two-pointer simulation. The brute-force rotated-string approach shows that you understand the digit mapping, but the two-pointer method demonstrates stronger problem-solving by eliminating extra space and validating symmetry in one pass.

Solution

We define an array d, where d[i] represents the number after rotating the digit i by 180°. If d[i] is -1, it means that the digit i cannot be rotated 180° to get a valid digit.

We define two pointers i and j, pointing to the left and right ends of the string, respectively. Then we continuously move the pointers towards the center, checking whether d[num[i]] and num[j] are equal. If they are not equal, it means that the string is not a strobogrammatic number, and we can directly return false. If i > j, it means that we have traversed the entire string, and we return true.

The time complexity is O(n), where n is the length of the string. The space complexity is O(1).

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Build Rotated String with Hash MapO(n)O(n)When clarity is preferred and constructing the rotated number helps visualize the transformation
Two Pointers SimulationO(n)O(1)Best approach for interviews; validates mirrored digits directly without extra memory

Video Solution

STROBOGRAMMATIC NUMBER | LEETCODE # 246 | PYTHON SOLUTIONCracking FAANG7,155 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Strobogrammatic Number easy or hard?
Strobogrammatic Number is classified as an Easy problem on LeetCode with an acceptance rate around 47%. The challenge is recognizing valid digit rotations and applying a two-pointer symmetry check efficiently.
Strobogrammatic Number Python/Java solution
In Python or Java, define a mapping of valid rotated digits and use two pointers moving inward. For each pair, verify that map[leftDigit] equals rightDigit. If any pair fails or a digit is not in the mapping, return false; otherwise return true after scanning the string.
How to solve Strobogrammatic Number in O(n)?
Use two pointers scanning from both ends of the string. Maintain a mapping of valid rotations: 0→0, 1→1, 6→9, 8→8, and 9→6. For each step, confirm that the left digit rotates to the right digit. Continue until the pointers cross; if all pairs match, the number is strobogrammatic.
What is the best approach for Strobogrammatic Number?
The two pointers simulation is the best approach. Start one pointer at the beginning and another at the end, then check whether each pair of digits forms a valid rotated pair such as (6,9) or (1,1). This method runs in O(n) time and uses O(1) space because it avoids building a new string.
Is Strobogrammatic Number asked at Google/Amazon/Meta?
Strobogrammatic Number appears in coding interview preparation lists and is commonly associated with companies like Google and Amazon in online interview archives. Variants such as generating all strobogrammatic numbers are also asked to test string manipulation and symmetry logic.
What data structure is used in Strobogrammatic Number?
A hash table (or dictionary) is typically used to store valid digit rotations such as 6→9 and 9→6. The algorithm then uses two pointers on the string to validate mirrored digits using constant-time hash lookups.
What is the time complexity of Strobogrammatic Number?
The optimal solution runs in O(n) time where n is the number of digits in the string. Each pair of digits is checked once using a constant-time lookup from a digit rotation map. Space complexity can be O(1) with the two-pointer method.

Ready to solve this problem?

Practice Strobogrammatic Number with our built-in code editor and test cases.

Practice on FleetCode