Skip to content

Commit a194bb5

Browse files
committed
Strobogrammatic Number
1 parent df4e4e2 commit a194bb5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Strobogrammatic_Number.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
2+
#
3+
# Write a function to determine if a number is strobogrammatic. The number is represented as a string.
4+
#
5+
# Example 1:
6+
#
7+
# Input: "69"
8+
# Output: true
9+
# Example 2:
10+
#
11+
# Input: "88"
12+
# Output: true
13+
# Example 3:
14+
#
15+
# Input: "962"
16+
# Output: false
17+
18+
19+
class Solution:
20+
def isStrobogrammatic(self, num):
21+
dict = {'6': '9', '9': '6', '8': '8', '0': '0', '1': '1'}
22+
23+
pointer = len(num) - 1
24+
nums = []
25+
while pointer >= 0:
26+
if num[pointer] not in dict:
27+
return False
28+
29+
nums.append(dict[num[pointer]])
30+
pointer -= 1
31+
32+
rotate = "".join(nums)
33+
34+
if rotate != num:
35+
return False
36+
37+
return True

0 commit comments

Comments
 (0)