#246
Easy Algorithms Strobogrammatic number
Hash Table Two Pointers String
47.5% acceptance
Mar 31, 2026
633
1052
Given a string num which represents an integer, return true if num is a strobogrammatic number.
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn is_strobogrammatic(num: String) -> bool {
let s: Vec<u8> = num.bytes().collect();
let n = s.len();
for i in 0..=(n - 1) / 2 {
let j = n - 1 - i;
let pair = (s[i], s[j]);
match pair {
(b'0', b'0') | (b'1', b'1') | (b'8', b'8') | (b'6', b'9') | (b'9', b'6') => {},
_ => return false,
}
}
true
}
}