Problem Number: 246 Difficulty: Easy Category: Hash Table, String, Two Pointers LeetCode Link: https://leetcode.com/problems/strobogrammatic-number/
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.
I used a Two Pointers approach with hash map mapping to check if a number is strobogrammatic. The key insight is to use a mapping of valid strobogrammatic digits and check from both ends.
Algorithm:
- Define mapping of valid strobogrammatic digits
- Use two pointers (left, right) starting from both ends
- While left <= right:
- Check if both digits are in mapping
- Check if left digit maps to right digit
- Move pointers inward
- Return True if all checks pass
The solution uses two pointers with hash map mapping to check strobogrammatic numbers. See the implementation in the solution file.
Key Points:
- Uses hash map for digit mapping
- Two-pointer approach from both ends
- Checks validity of each digit pair
- Handles odd and even length numbers
Time Complexity: O(n)
- Single pass through the string
- Each iteration performs constant time operations
- Total: O(n)
Space Complexity: O(1)
- Hash map has constant size (5 mappings)
- Total: O(1)
-
Digit Mapping: Only certain digits (0,1,6,8,9) are valid strobogrammatic.
-
Two Pointers: Using two pointers from both ends is efficient.
-
Pair Checking: Each pair of digits must map correctly.
-
Center Digit: For odd length, center digit must map to itself.
-
Valid Digits: Only 0,1,6,8,9 are valid strobogrammatic digits.
-
Mapping Rules: 6↔9, 0↔0, 1↔1, 8↔8.
-
Wrong Mapping: Initially might use incorrect digit mappings.
-
Complex Logic: Overcomplicating the strobogrammatic check.
-
Wrong Order: Not checking digits in correct order.
-
Missing Digits: Not including all valid strobogrammatic digits.
- Strobogrammatic Number II (Problem 247): Generate strobogrammatic numbers
- Strobogrammatic Number III (Problem 248): Count strobogrammatic numbers in range
- Valid Palindrome (Problem 125): Check if string is palindrome
- Palindrome Number (Problem 9): Check if number is palindrome
- String Reversal: Reverse string and apply mapping - O(n) time, O(n) space
- Single Pass: Use single pointer with length calculation - O(n) time, O(1) space
- Recursive: Use recursion to check pairs - O(n) time, O(n) space
- Wrong Mapping: Using incorrect digit mappings.
- Complex Logic: Overcomplicating the strobogrammatic check.
- Wrong Order: Not checking digits in correct order.
- Missing Digits: Not including all valid strobogrammatic digits.
- Center Handling: Not properly handling center digit for odd length.
Note: This is a two-pointer problem that demonstrates efficient strobogrammatic number checking with digit mapping.