-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0880-decoded-string-at-index.js
46 lines (42 loc) · 1.11 KB
/
0880-decoded-string-at-index.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
/**
* 880. Decoded String at Index
* https://leetcode.com/problems/decoded-string-at-index/
* Difficulty: Medium
*
* You are given an encoded string s. To decode the string to a tape, the encoded string is read
* one character at a time and the following steps are taken:
* - If the character read is a letter, that letter is written onto the tape.
* - If the character read is a digit d, the entire current tape is repeatedly written d - 1
* more times in total.
*
* Given an integer k, return the kth letter (1-indexed) in the decoded string.
*/
/**
* @param {string} s
* @param {number} k
* @return {string}
*/
var decodeAtIndex = function(s, k) {
let tapeLength = 0;
let i = 0;
while (tapeLength < k) {
if (isLetter(s[i])) {
tapeLength++;
} else {
tapeLength *= parseInt(s[i]);
}
i++;
}
while (i--) {
if (isLetter(s[i])) {
if (tapeLength === k) return s[i];
tapeLength--;
} else {
tapeLength = Math.floor(tapeLength / parseInt(s[i]));
k = k % tapeLength || tapeLength;
}
}
};
function isLetter(char) {
return /[a-z]/.test(char);
}