# Strobogrammatic Number

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string.

According to Wikipedia “A Strobogrammatic Number is a number whose numeral is rotationally symmetric so that it appears the same when rotated 180 degrees.

Example 1:

```Input:  "69"
Output: true
```

Example 2:

```Input:  "88"
Output: true```

Example 3:

```Input:  "962"
Output: false```

Constraints:

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

The first few strobogrammatic numbers are:

```0, 1,  8, 11, 69, 88,  96, 101, 111, 181, 609,
619, 689, 808, 818, 888,  906, 916, 986, 1001,
1111,1691, 1881, 1961, 6009, 6119, 6699, 6889,
6969, 8008, 8118, 8698, 8888, 8968, 9006, 9116,
9696, 9886, 9966, ...```

### Check the following figure to get more clarification.

For the number to be strobogrammatic, the number should be of following pairs.

1. `0` and `0`.
2. `1` and `1`.
3. `6` and `9`.
4. `8` and `8`.
5. `9` and `6`.

Therefore, we can check each pair that would swap in the reversal for whether or not it is one of the five pairs listed above. If all pairs are on the list, then the number is strobogrammatic. For odd-lengthed numbers, the middle digit has to be `0``1`, or `8`.

Java Code:

```class Solution {
public boolean isStrobogrammatic(String num) {

Map<Integer, Integer> strobogrammaticDictonary = new HashMap<>();
strobogrammaticDictonary.put(0,0);
strobogrammaticDictonary.put(1,1);
strobogrammaticDictonary.put(6,9);
strobogrammaticDictonary.put(8,8);
strobogrammaticDictonary.put(9,6);

int n = num.length();
for(int i = 0 , j = (n-1) ; i <= j ; i++, j--){
int digit_left = num.charAt(i) - '0';
int digit_right = num.charAt(j) - '0';

int mapping = strobogrammaticDictonary.getOrDefault(digit_left, -1);
if(mapping == -1){
return false;
}
if(mapping != digit_right){
return false;
}
}
return true;
}
}
```

Categories: Map

Tagged as: