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.
Input: "962"
Output: false
bool isStrobogrammatic(string num) { // time: O(n); space: O(1)
string rotate(10, ' ');
rotate['0' - '0'] = '0';
rotate['1' - '0'] = '1';
rotate['6' - '0'] = '9';
rotate['8' - '0'] = '8';
rotate['9' - '0'] = '6';
int l = 0, r = num.length() - 1;
while (l <= r) {
if (rotate[num[l] - '0'] != num[r]) return false;
++l, --r;
}
return true;
}
bool isStrobogrammatic(string num) { // time: O(n); space: O(1)
const string pattern = "00 11 88 696";
string search;
for (int i = 0, j = num.length() - 1; i <= j; ++i, --j) {
search.clear();
search.push_back(num[i]);
search.push_back(num[j]);
if (pattern.find(search) == string::npos) return false;
}
return true;
}