Skip to content

Commit 707e4a9

Browse files
authored
Create 2399-check-distances-between-same-letters.js
1 parent 673b725 commit 707e4a9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} s
3+
* @param {number[]} distance
4+
* @return {boolean}
5+
*/
6+
var checkDistances = function(s, distance) {
7+
const hash = {}
8+
const a = 'a'.charCodeAt(0)
9+
const n = s.length
10+
for(let i = 0; i < n; i++) {
11+
if(hash[s[i]] == null) hash[s[i]] = []
12+
hash[s[i]].push(i)
13+
}
14+
const keys = Object.keys(hash)
15+
for(let i = 0; i < keys.length; i++) {
16+
const k = keys[i]
17+
const idx = k.charCodeAt(0) - a
18+
if(hash[k][1] - hash[k][0] !== distance[idx] + 1) return false
19+
}
20+
return true
21+
};

0 commit comments

Comments
 (0)