Accepted Solution in C++ using unordered map and two pointers


  • 0
    H
    bool isStrobogrammatic(string num) {
    /*
    all possible pairs:
    (1,1)
    (8,8)
    (6,9)
    (0,0)
    */
     unordered_map<char, char> mypair;
     mypair['0'] = '0';
     mypair['1'] = '1';
     mypair['8'] = '8';
     mypair['6'] = '9';
     mypair['9'] = '6';
     int front = 0;
     int back = num.size() - 1;
     while (front <= back) {
         if (mypair.find(num[front]) != mypair.end() &&
                mypair.find(num[back]) != mypair.end() ) {
             if (mypair[num[front]] != num[back]) {
                return false;
            }
    
        } else {
            return false;
        }
        front++;
        back--;
    
     }
     return true;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.