forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrobogramattic.java
40 lines (31 loc) · 1 KB
/
Strobogramattic.java
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
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
Example 1:
Input: "69"
Output: true
Example 2:
Input: "88"
Output: true
INTUITON, make a map of all th valid keys and their opposite value
then use, TWO POINTERS to check from start and end if they are equal
//TC: O(N) to go through the length of the string
//SC: O(1) because constant map space
class Solution {
public boolean isStrobogrammatic(String num) {
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('6', '9');
map.put('9', '6');
map.put('0', '0');
map.put('1', '1');
map.put('8', '8');
int l = 0, r = num.length() - 1;
while (l <= r) {
if (!map.containsKey(num.charAt(l))) return false;
if (map.get(num.charAt(l)) != num.charAt(r))
return false;
l++;
r--;
}
return true;
}
}