246. Strobogrammatic Number

Problem:

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.

Example 1:

Input:  "69"
Output: true

Example 2:

Input:  "88"
Output: true

Example 3:

Input:  "962"
Output: false

Solutions:

class Solution {
public:
    bool isStrobogrammatic(string num) {
        unordered_map<char, char> mirror {
            {'0', '0'},
            {'1', '1'},
            {'6', '9'},
            {'8', '8'},
            {'9', '6'}
        };

        int left = 0;
        int right = num.length() - 1;
        while (left <= right) {
            if (mirror.count(num[left]) == 0 || mirror[num[left]] != num[right])    return false;
            ++left;
            --right;
        }

        return true;
    }
};

results matching ""

    No results matching ""