Strobogrammatic Number solution and explanation
9 more videos on this problem
STROBOGRAMMATIC NUMBER | LEETCODE # 246 | PYTHON SOLUTIONCracking FAANG · 9:50 · 7K views
Strobogrammatic Number Problem - LeetCode 246Scott Hacks Code · 4:01 · 1K views
leetcode - 246 Strobogrammatic NumberlitCode · 5:42 · 1K views
LeetCode in Python 246. Strobogrammatic Number - Michelle小梦想家Michelle小梦想家 · 12:08 · 1K views
Leetcode 246. Strobogrammatic NumberAlgorithms Casts · 28:08 · 1K views
246. Strobogrammatic Number (LeetCode)hakunamatasq · 10:44 · 945 views
246. Strobogrammatic Number - Week 4/5 Leetcode November ChallengeProgramming Live with Larry · 3:41 · 850 views
Strobogrammatic Number | Leetcode 246 | FAANG Interview QuestionCodeSharpener · 8:05 · 689 views
Facebook Coding Interview Question | Leetcode 246 | Strobogrammatic NumberWorkWithGoogler · 11:39 · 342 views
246. Strobogrammatic Number - English VersionSimple Code - 단순코드 · 5:14 · 236 views
The problem
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 <= 50numconsists of only digits.numdoes 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).
class Solution:
def isStrobogrammatic(self, num: str) -> bool:
d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
i, j = 0, len(num) - 1
while i <= j:
a, b = int(num[i]), int(num[j])
if d[a] != b:
return False
i, j = i + 1, j - 1
return Trueclass Solution {
public boolean isStrobogrammatic(String num) {
int[] d = new int[] {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
for (int i = 0, j = num.length() - 1; i <= j; ++i, --j) {
int a = num.charAt(i) - '0', b = num.charAt(j) - '0';
if (d[a] != b) {
return false;
}
}
return true;
}
}class Solution {
public:
bool isStrobogrammatic(string num) {
vector<int> d = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
for (int i = 0, j = num.size() - 1; i <= j; ++i, --j) {
int a = num[i] - '0', b = num[j] - '0';
if (d[a] != b) {
return false;
}
}
return true;
}
};func isStrobogrammatic(num string) bool {
d := []int{0, 1, -1, -1, -1, -1, 9, -1, 8, 6}
for i, j := 0, len(num)-1; i <= j; i, j = i+1, j-1 {
a, b := int(num[i]-'0'), int(num[j]-'0')
if d[a] != b {
return false
}
}
return true
}Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Build Rotated String with Hash Map | O(n) | O(n) | When clarity is preferred and constructing the rotated number helps visualize the transformation |
| Two Pointers Simulation | O(n) | O(1) | Best approach for interviews; validates mirrored digits directly without extra memory |
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.
