
Problem Statement
In computational problems involving strings and numbers, a unique and interesting category is that of the "strobogrammatic number". A number is considered strobogrammatic if it retains the same visual representation when rotated 180 degrees—effectively when it is turned upside down. The task is to determine whether a given string, that represents an integer, qualifies as a strobogrammatic number.
For instance, the number '69' when flipped appears as '96', which is the same set of digits but in reverse order. Thus, identifying whether a number is strobogrammatic involves examining its readability post this rotation.
Examples
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 and Intuition
Let's dissect the nature and solution approach for this problem through the given examples and constraints:
Understanding strobogrammatic digits:
- Only certain digits look like valid digits when rotated 180 degrees. These include: 0, 1, 8 which look the same, and pairs like 6 and 9 which transform into each other.
- Digits such as 2, 3, 4, 5, 7 do not form valid digits upon being flipped and hence, cannot be part of a strobogrammatic number unless paired correctly (like 6 with 9).
Solution methodology using examples:
- Example 1: '69'
- '6' turns to '9', and '9' turns to '6'. When flipped, '69' becomes '96', which is the reverse of the original number. Hence, the output is
true
.
- '6' turns to '9', and '9' turns to '6'. When flipped, '69' becomes '96', which is the reverse of the original number. Hence, the output is
- Example 2: '88'
- Both '8's remain '8' when rotated. Thus, the flipped number is still '88', mirroring the original. Therefore, the output is
true
.
- Both '8's remain '8' when rotated. Thus, the flipped number is still '88', mirroring the original. Therefore, the output is
- Example 3: '962'
- While '9' turns to '6' and '6' turns to '9', '2' does not represent a valid strobogrammatic digit. Thus, the number cannot maintain its strobogrammatic nature. Therefore, the output is
false
.
- While '9' turns to '6' and '6' turns to '9', '2' does not represent a valid strobogrammatic digit. Thus, the number cannot maintain its strobogrammatic nature. Therefore, the output is
- Example 1: '69'
Constraints review and edge cases:
- The length of
num
can be up to 50, which means it's pertinent to consider performance, but the operation of checking pairs remains lightweight. - Inputs will not suffer from complications like non-digit characters or inappropriate leading zeros, avoiding unnecessary preprocessing.
- The length of
In summary, to determine if a number represented as a string is strobogrammatic, one would:
- Check each digit and its corresponding opposite-end digit in the string.
- Verify if they are valid strobogrammatic pairs.
- Continue this across the string’s length; if all are valid pairs, then the string as a whole is strobogrammatic.
Solutions
- Java
- Python
class Solution {
public boolean checkStrobogrammatic(String num) {
Map<Character, Character> reversibleNumbers = new HashMap<> (
Map.of('0', '0', '1', '1', '6', '9', '8', '8', '9', '6'));
for (int start = 0, end = num.length() - 1; start <= end; start++, end--) {
char startChar = num.charAt(start);
char endChar = num.charAt(end);
if (!reversibleNumbers.containsKey(startChar) || reversibleNumbers.get(startChar) != endChar) {
return false;
}
}
return true;
}
}
In this solution, you implement a Java method to determine if a number is strobogrammatic—a number that appears the same when rotated 180 degrees. The method is named checkStrobogrammatic
and is a member of the Solution
class.
The function begins by mapping characters to their respective strobogrammatic counterparts using a
HashMap
calledreversibleNumbers
. The map's entries include:- '0' maps to '0'
- '1' maps to '1'
- '6' maps to '9'
- '8' maps to '8'
- '9' maps to '6'
Loop through the string from both start and end using two pointers (
start
andend
).- Iteration continues while
start
is less than or equal toend
. - For each character at the
start
index, the character at theend
index should be its mapped counterpart for the number to be strobogrammatic. - If any pair of characters fails this check, return
false
.
- Iteration continues while
If the loop completes without finding any non-strobogrammatic characters, return
true
, indicating the number is strobogrammatic.
This implementation efficiently checks each required character pair once, resulting in O(n) runtime complexity where n is the length of the number, and uses a constant amount of additional space for the map.
class Solution:
def checkStrobogrammatic(self, number: str) -> bool:
flip_mapping = {'0': '0', '1': '1', '8': '8', '6': '9', '9': '6'}
start = 0
end = len(number) - 1
while start <= end:
if number[start] not in flip_mapping \
or flip_mapping[number[start]] != number[end]:
return False
start += 1
end -= 1
return True
This summary explains a solution for calculating whether a number is strobogrammatic, meaning it looks the same when rotated 180 degrees. The solution is implemented in Python.
Ensure you initiate a mapping between each digit and its counterpart when flipped. Use '0'
, '1'
, '8'
to map to themselves and '6'
maps to '9'
, '9'
to '6'
. Set up two pointers, one starting from the beginning (start
) of the string representation of the number, and the other from the end (end
). Iterate with these pointers moving towards each other. Check the following conditions:
- If the current digit at
start
or its respective end counterpart (end
) does not exist in the predefined flip mapping dictionary or if the mapped value of thestart
digit does not match the digit at positionend
, the function returnsFalse
. - Adjust the pointers after each iteration with
start
increasing by one andend
decreasing by one. - If all pairs of digits across the number satisfy the strobogrammatic condition, the function returns
True
, confirming the number is strobogrammatic.
By adhering to this solution, successfully determine the strobogrammatic nature of any number provided as a string.
No comments yet.