Skip to content

Commit 26ca794

Browse files
solves #246: Strobogrammatic Number in java
1 parent b4f3197 commit 26ca794

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@
215215
| 243 | 🔒 [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance) | [![Java](assets/java.png)](src/ShortestWordDistance.java) | |
216216
| 244 | 🔒 [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii) | | |
217217
| 245 | 🔒 [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii) | | |
218-
| 246 | 🔒 [Strobogramatic Number](https://leetcode.com/problems/strobogrammatic-number) | | |
219-
| 247 | 🔒 [Strobogramatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii) | | |
218+
| 246 | 🔒 [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number) | [![Java](assets/java.png)](src/StrobogrammaticNumber.java) | |
219+
| 247 | 🔒 [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii) | | |
220220
| 249 | 🔒 [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings) | | |
221221
| 250 | 🔒 [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees) | | |
222222
| 251 | 🔒 [Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector) | | |

src/StrobogrammaticNumber.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://leetcode.com/problems/strobogrammatic-number
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
// Strobogrammatic digits: 0, 1, 8
9+
// Strobogrammatic interchangeable digits: 6 --> 9, 9 --> 6
10+
public class StrobogrammaticNumber {
11+
private static final Map<Character, Character> STROBOGRAMMATIC_MIRROR = new HashMap<>() {{
12+
put('0', '0');
13+
put('1', '1');
14+
put('6', '9');
15+
put('8', '8');
16+
put('9', '6');
17+
}};
18+
19+
public boolean isStrobogrammatic(String number) {
20+
for (int i = 0 ; i < number.length() ; i++) {
21+
final char digit = number.charAt(i);
22+
if (!STROBOGRAMMATIC_MIRROR.containsKey(digit)
23+
|| STROBOGRAMMATIC_MIRROR.get(digit) != number.charAt(number.length() - 1 - i)
24+
) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)