forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrobogrammatic-number.js
More file actions
38 lines (34 loc) · 776 Bytes
/
strobogrammatic-number.js
File metadata and controls
38 lines (34 loc) · 776 Bytes
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
/**
* Strobogrammatic Number
*
* 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
* Example 3:
*
* Input: "962"
* Output: false
*/
/**
* @param {string} num
* @return {boolean}
*/
const isStrobogrammatic = num => {
const map = new Map([['6', '9'], ['9', '6'], ['0', '0'], ['1', '1'], ['8', '8']]);
for (let i = 0, j = num.length - 1; i <= j; i++, j--) {
if (!map.has(num[i]) || map.get(num[i]) !== num[j]) {
return false;
}
}
return true;
};
export { isStrobogrammatic };