Date: 2026-06-06
Time: 19:17
strobogrammatic-number/solution.pyThis file solves LeetCode 246 — Strobogrammatic Number. A strobogrammatic number looks the same when rotated 180 degrees (flipped upside down). The file owns a single responsibility: determining whether a given numeric string has this rotational symmetry property.
Solution.isStrobogrammatic(self, num: str) -> bool
The sole method. It takes a string of digits and returns whether it reads identically after a 180-degree rotation.
mapping dict — The core lookup table encoding which digits are valid under rotation and what they map to:
| Digit | Rotated |
|-------|---------|
| 0 | 0 |
| 1 | 1 |
| 6 | 9 |
| 8 | 8 |
| 9 | 6 |
Digits 2, 3, 4, 5, 7 are absent — any string containing them is automatically non-strobogrammatic because the num[left] not in mapping check fails.
Two-pointer inward sweep — Classic palindrome-style traversal. left starts at 0, right at the last index, and they converge toward the center. This naturally handles both odd-length strings (where the middle digit must map to itself — only 0, 1, 8 qualify) and even-length strings.
The check mapping[num[left]] != num[right] is the key insight: unlike a palindrome where s[i] == s[n-1-i], strobogrammatic numbers require rotate(s[i]) == s[n-1-i]. The mapping encodes the rotation.
Imports: None. Pure self-contained logic with no stdlib or third-party dependencies.
Imported by: The test_solution.py in the same directory. The "Imported By" list in the prompt is misleading — those are unrelated test files in sibling problem directories that happen to share import machinery, not actual consumers of this solution.
1. Build the rotation mapping (5 entries).
2. Initialize two pointers at string boundaries.
3. Loop while left <= right:
num[left] isn't a rotatable digit → return False.num[left] doesn't match num[right] → return False.4. If the loop completes without failing → return True.
The <= in the loop condition (not <) is critical: for odd-length strings, the center character is compared against itself via mapping[num[mid]] != num[mid], which correctly rejects 6 or 9 at the center (since mapping['6'] == '9' != '6').
num must exist in mapping, or the number is rejected.(i, n-1-i), the rotated value of num[i] must equal num[n-1-i].None. The method assumes valid input (a non-empty string of digit characters), consistent with LeetCode's problem contract. No exceptions are raised or caught.