-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC246-Strobogrammatic-Number.py
69 lines (60 loc) · 1.57 KB
/
LC246-Strobogrammatic-Number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
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).
Example 1:
Input: num = "69"
Output: true
Example 2:
Input: num = "88"
Output: true
Example 3:
Input: num = "962"
Output: false
Example 4:
Input: num = "1"
Output: true
Constraints:
(*) 1 <= num.length <= 50
(*) num consists of only digits.
(*) num does not contain any leading zeros except for zero itself.
"""
class Solution:
def isStrobogrammatic(self, num: str) -> bool:
"""
Runtime complexity: O(n), where n in the number of digits in num, i.e., log(int(num))
Space complexity: O(1)
"""
i_start = 0
i_end = len(num) - 1
while i_start <= i_end:
if num[i_start] + num[i_end] not in ['00', '11', '69', '88', '96']:
return False
i_start += 1
i_end -= 1
return True
if __name__ == '__main__':
from run_tests import run_tests
correct_answers = [
['1', True],
['2', False],
['3', False],
['4', False],
['5', False],
['6', False],
['7', False],
['8', True],
['9', False],
['0', True],
['11', True],
['111', True],
['88', True],
['89', False],
['81', False],
['80', False],
['69', True],
['669', False]
]
print(f'Running tests for isStrobogrammatic')
run_tests(Solution().isStrobogrammatic, correct_answers)