Skip to content

Commit 0ce2abe

Browse files
authored
Create 1165-single-row-keyboard.js
1 parent e56488e commit 0ce2abe

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

1165-single-row-keyboard.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} keyboard
3+
* @param {string} word
4+
* @return {number}
5+
*/
6+
const calculateTime = function(keyboard, word) {
7+
const hash = {}, { abs } = Math
8+
for(let i = 0; i < 26; i++) {
9+
hash[keyboard[i]] = i
10+
}
11+
let pre = 0, sum = 0
12+
for(const ch of word) {
13+
sum += abs(hash[ch] - pre)
14+
pre = hash[ch]
15+
}
16+
return sum
17+
};

0 commit comments

Comments
 (0)