-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__E__distanceCalculate.js
48 lines (41 loc) · 1.17 KB
/
__E__distanceCalculate.js
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
// https: //leetcode.com/problems/check-distances-between-same-letters/
const c = console.log.bind(console);
var checkDistances = function(s, distance) {
let map = new Map();
map.set(22, 32);
// return String.fromCharCode(97)
for (let i = 0; i < 27; i++) {
map.set(String.fromCharCode(97 + i), i);
}
let distinctChar = [...new Set(s.split(""))];
for (let q of distinctChar) {
let indexes = [];
s.split("").filter((item, i) => {
item == q ? indexes.push(i) : "";
});
let spaces = indexes[1] - indexes[0] - 1;
// c(spaces)
if (distance[map.get(q)] != spaces) {
return false;
} else {
indexes = [];
}
// c(indexes);
}
return true;
};
//few lines solutions from community
const checkDistances = (s, distance) => {
for (let c of new Set(s))
if (s.lastIndexOf(c) - s.indexOf(c) - 1 !== distance[c.charCodeAt(0) - 97])
return false;
return true;
};
c(
checkDistances(
"abaccb", [
1, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
]
)
);