Search
⌘K

Leetcode 246. Strobogrammatic Number

Determine whether a numeric string is strobogrammatic — i.e., it appears the same when rotated 180 degrees using the digit mappings 0↔0, 1↔1, 6↔9, and 8↔8. This requires checking that each pair of symmetric characters across the string matches the strobogrammatic mapping.

Asked at:

Meta


DESCRIPTION

Determine whether a numeric string is strobogrammatic — i.e., it appears the same when rotated 180 degrees using the digit mappings 0↔0, 1↔1, 6↔9, and 8↔8. This requires checking that each pair of symmetric characters across the string matches the strobogrammatic mapping.

Input:

num = "619"

Output:

true


Explanation: When rotated 180 degrees, '6' becomes '9' and '9' becomes '6', and reading backwards gives us '619' again, which matches the original

Constraints:

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

Understanding the Problem

Let's understand what we're being asked to do. We have a numeric string like '69' and need to check if it looks the same when rotated 180 degrees.

Tracing through '69': When we rotate 180 degrees, '6' becomes '9' and '9' becomes '6', so we get '96'. But wait - we also need to reverse the order! After rotation, the string reads backwards. So '69' rotated is '69' (reading the rotated digits from left to right). This matches the original!

Key insight: Only certain digits are valid after rotation: '0' stays '0', '1' stays '1', '6' becomes '9', '8' stays '8', and '9' becomes '6'. Any other digit (like '2', '3', '4', '5', '7') makes the string invalid.

Edge cases to consider: What about '88'? (valid - both '8's stay '8'). What about '962'? (invalid - '2' has no valid rotation). What about single digit '0'? (valid). What about '6' alone? (invalid - rotates to '9', doesn't match).

Building Intuition

When we rotate a string 180 degrees, two things happen: each digit transforms according to the mapping (0↔0, 1↔1, 6↔9, 8↔8), AND the string reads backwards.

This means the first character must match (after rotation) with the last character, the second with the second-to-last, and so on. We're checking symmetric pairs from both ends moving inward.

Instead of rotating the entire string and comparing, we can check pairs of characters at symmetric positions. The character at index i must be the rotated version of the character at index n-1-i.

This transforms the problem from string manipulation into a two-pointer validation problem, checking pairs from outside to inside.

Imagine holding a piece of paper with '6889' written on it. Rotate it 180 degrees - you're now looking at it upside-down and backwards.

The '6' (first character) is now in the last position and looks like '9'. The last character '9' is now in the first position and looks like '6'. The two middle '8's swap positions but still look like '8's.

For the string to be strobogrammatic, each pair of symmetric positions must satisfy the rotation mapping. We can verify this by checking from both ends simultaneously.

Common Mistakes

Optimal Solution

Use two pointers starting from both ends of the string, moving inward. For each pair of characters at symmetric positions, verify that they form a valid strobogrammatic pair according to the mapping (0↔0, 1↔1, 6↔9, 8↔8). The left character must map to the right character under 180-degree rotation. If any pair fails this check, or if any character isn't in the valid set, return false. If all pairs pass, return true.

|
string
Visualization
def is_strobogrammatic(num):
"""
Check if a numeric string is strobogrammatic (looks same when rotated 180 degrees).
Uses two-pointer approach to verify symmetric digit pairs.
"""
# Define strobogrammatic digit mappings
strobo_map = {
'0': '0',
'1': '1',
'6': '9',
'8': '8',
'9': '6'
}
# Initialize two pointers
left = 0
right = len(num) - 1
# Check each symmetric pair from both ends
while left <= right:
left_char = num[left]
right_char = num[right]
# Check if left character is valid strobogrammatic digit
if left_char not in strobo_map:
return False
# Check if left character maps to right character
if strobo_map[left_char] != right_char:
return False
# Move pointers inward
left += 1
right -= 1
# All pairs matched successfully
return True
619012

Start checking if string is strobogrammatic

0 / 11

Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(n) We iterate through half the string once with two pointers, checking each pair of symmetric characters exactly once, resulting in linear time complexity

Space Complexity: O(1) We only use two pointer variables and constant space for the rotation mapping lookup, regardless of input size

What We've Learned

  • Two-pointer symmetry pattern: When validating palindrome-like properties with transformations, use two pointers from opposite ends moving inward - this naturally checks symmetric pairs and halves the work by examining each character only once against its mirror position.
  • HashMap for bidirectional mappings: Store rotation mappings in a hash table/dictionary (0→0, 1→1, 6→9, 8→8, 9→6) rather than multiple conditionals - this makes the code extensible and allows O(1) lookup for valid digit transformations.
  • Single-pass validation: Combine existence checking and value matching in one pass - verify both that `num[left]` exists in the mapping AND that it maps to `num[right]`, avoiding the need for separate validation loops and reducing time complexity to O(n/2).
  • Center character edge case: For odd-length strings, the middle character must map to itself (0, 1, or 8 only) - forgetting this causes false positives since 6 and 9 are valid in pairs but invalid at the center position where they'd need to map to themselves.
  • Character-level rotation problems: This two-pointer + mapping pattern extends to any problem involving character transformations with positional symmetry - mirror words, rotational ciphers, or validating strings under custom character substitution rules.
  • Early termination optimization: Return false immediately when encountering an invalid digit (2, 3, 4, 5, 7) or mismatched pair - this short-circuit evaluation avoids unnecessary comparisons and is especially efficient for invalid inputs, which fail fast rather than checking the entire string.

Related Concepts and Problems to Practice

Palindrome Linked List

easy

Linked List

This problem uses the same two-pointer technique to check symmetry by comparing elements from both ends moving inward, similar to checking if a string reads the same forwards and backwards with rotation mappings.

Both problems involve character-by-character validation with specific pairing rules (parentheses matching vs digit rotation mappings) and checking symmetry or valid pairings throughout the string.

Overview
Lesson
Two Pointers

The strobogrammatic number problem is fundamentally a two-pointer problem where you compare characters from the start and end moving inward. This lesson teaches the core two-pointer pattern used in the solution.

Test Your Understanding

Why is array the right data structure for this problem?

1

array provides the optimal access pattern

2

It's the only data structure that works

3

It's the easiest to implement

4

It uses the least memory

Select an answer to see the explanation

Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Late November, 2025

Meta

Senior

Late November, 2025

Meta

Senior

Mid November, 2025

Meta

Senior

Comments

Your account is free and you can post anonymously if you choose.