-
Notifications
You must be signed in to change notification settings - Fork 0
/
1165-singleRowKeyboard.ts
68 lines (48 loc) · 2.15 KB
/
1165-singleRowKeyboard.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// ---------------- LEETCODE TASK DESCRIPTION ----------------- //
// ---------------- 1165. Single-Row Keyboard ----------------- //
/* There is a special keyboard with all keys in a single row.
Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25). Initially, your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|.
You want to type a string word. Write a function to calculate how much time it takes to type it with one finger.
Example 1:
Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output: 4
Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'.
Total time = 2 + 1 + 1 = 4.
Example 2:
Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
Output: 73 */
// ------------------------ SOLUTION -------------------------- //
// Setting up map of keyboard
// Setting up result variable
// Setting up last index variable
// Setting up current index variable
// Iterate through word
// Set current index to map.get(word[i])
// Set num to Math.abs(lastIndex - currentIndex)
// Set lastIndex to currentIndex
// Add num to result
// Return result
// Time complexity: O(n)
// Space complexity: O(n)
const calculateTime = (keyboard: string, word: string): number => {
const map = new Map()
for (let i = 0; i < keyboard.length; i++) {
map.set(keyboard[i], i)
}
let result = 0
let lastIndex = 0
let currentIndex = 0
for (let i = 0; i < word.length; i++) {
currentIndex = map.get(word[i])
let num = Math.abs(lastIndex - currentIndex)
lastIndex = currentIndex
result += num
}
return result
};
// --------------------------- LOGS --------------------------- //
console.log(calculateTime("abcdefghijklmnopqrstuvwxyz", "cba"))
console.log(calculateTime("pqrstuvwxyzabcdefghijklmno", "leetcode"))
console.log(calculateTime("pqrstuvwxyzabcdefghijklmno", "pqrstuvwxyzabcdefghijklmno"))
// ----------------------- CLI COMMAND ------------------------ //
//run ts-node 1165-singleRowKeyboard.ts