forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_strobogrammatic.py
More file actions
58 lines (44 loc) · 1.38 KB
/
is_strobogrammatic.py
File metadata and controls
58 lines (44 loc) · 1.38 KB
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
"""
Strobogrammatic Number Check
Determine whether a number (as a string) is strobogrammatic, meaning it
looks the same when rotated 180 degrees.
Reference: https://en.wikipedia.org/wiki/Strobogrammatic_number
Complexity:
Time: O(n) where n is the length of the number string
Space: O(1) for is_strobogrammatic, O(n) for is_strobogrammatic2
"""
from __future__ import annotations
def is_strobogrammatic(num: str) -> bool:
"""Check if a number string is strobogrammatic using two pointers.
Args:
num: String representation of the number.
Returns:
True if num is strobogrammatic, False otherwise.
Examples:
>>> is_strobogrammatic("69")
True
>>> is_strobogrammatic("14")
False
"""
comb = "00 11 88 69 96"
i = 0
j = len(num) - 1
while i <= j:
if comb.find(num[i] + num[j]) == -1:
return False
i += 1
j -= 1
return True
def is_strobogrammatic2(num: str) -> bool:
"""Check if a number string is strobogrammatic using string reversal.
Args:
num: String representation of the number.
Returns:
True if num is strobogrammatic, False otherwise.
Examples:
>>> is_strobogrammatic2("69")
True
>>> is_strobogrammatic2("14")
False
"""
return num == num[::-1].replace("6", "#").replace("9", "6").replace("#", "9")