LeetCode Solutions

246. Strobogrammatic Number

Time: $O(n)$

Space: $O(10) = O(1)$

			

class Solution {
 public:
  bool isStrobogrammatic(string num) {
    const vector<char> rotated{'0', '1', 'x', 'x', 'x',
                               'x', '9', 'x', '8', '6'};
    int l = 0;
    int r = num.length() - 1;

    while (l <= r)
      if (num[l++] != rotated[num[r--] - '0'])
        return false;

    return true;
  }
};
			

class Solution {
  public boolean isStrobogrammatic(String num) {
    final char[] rotated = {'0', '1', 'n', 'n', 'n', 'n', '9', 'n', '8', '6'};
    int l = 0;
    int r = num.length() - 1;

    while (l <= r)
      if (num.charAt(l++) != rotated[num.charAt(r--) - '0'])
        return false;

    return true;
  }
}
			

class Solution:
  def isStrobogrammatic(self, num: str) -> bool:
    rotated = ['0', '1', 'x', 'x', 'x',
               'x', '9', 'x', '8', '6']
    l = 0
    r = len(num) - 1

    while l <= r:
      if num[l] != rotated[ord(num[r]) - ord('0')]:
        return False
      l += 1
      r -= 1

    return True