225. Strobogrammatic Number
Imagine writing a number on paper and spinning the page a full 180°. A few digits survive the spin: 0, 1, and 8 still look like themselves, while 6 and 9 swap into each other. The digits 2, 3, 4, 5, and 7 turn into meaningless shapes.
A number is strobogrammatic when the rotated page shows exactly the same number you started with. You are given num, a string of digits; return true if num is strobogrammatic and false otherwise. Note that rotating reverses the digit order too — the first digit lands where the last one was — so each digit must rotate into its mirror partner across the string.
Example 1:
Input: num = "69"
Output: true
Explanation: Rotated, the 9 lands first and becomes 6, and the 6 lands last and becomes 9 — the page still reads "69".
Example 2:
Input: num = "88"
Output: true
Explanation: Each 8 rotates into an 8 and the two swap positions, leaving "88" unchanged.
Example 3:
Input: num = "962"
Output: false
Explanation: The digit 2 has no readable 180° rotation, so the number cannot be strobogrammatic.
Constraints:
- 1 ≤ num.length ≤ 50
- num consists of the characters '0' through '9' only
Hints:
Write down the full rotation table first: 0→0, 1→1, 8→8, 6→9, 9→6, and everything else is invalid. Any digit outside the table sinks the whole number immediately.
Rotation reverses the string as well as flipping each digit, so position i must pair with position n-1-i. Walk two pointers inward and check rot(num[left]) == num[right].
Do not forget the middle character of an odd-length string: the pointers meet there, and the check rot(mid) == mid correctly rejects a lone 6 or 9.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: num = "69"
Expected output: true