Skip to content

Commit 63c7927

Browse files
committedMar 22, 2025
Add solution #880
1 parent d29b517 commit 63c7927

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@
690690
877|[Stone Game](./0877-stone-game.js)|Medium|
691691
878|[Nth Magical Number](./0878-nth-magical-number.js)|Hard|
692692
879|[Profitable Schemes](./0879-profitable-schemes.js)|Hard|
693+
880|[Decoded String at Index](./0880-decoded-string-at-index.js)|Medium|
693694
884|[Uncommon Words from Two Sentences](./0884-uncommon-words-from-two-sentences.js)|Easy|
694695
889|[Construct Binary Tree from Preorder and Postorder Traversal](./0889-construct-binary-tree-from-preorder-and-postorder-traversal.js)|Medium|
695696
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 880. Decoded String at Index
3+
* https://leetcode.com/problems/decoded-string-at-index/
4+
* Difficulty: Medium
5+
*
6+
* You are given an encoded string s. To decode the string to a tape, the encoded string is read
7+
* one character at a time and the following steps are taken:
8+
* - If the character read is a letter, that letter is written onto the tape.
9+
* - If the character read is a digit d, the entire current tape is repeatedly written d - 1
10+
* more times in total.
11+
*
12+
* Given an integer k, return the kth letter (1-indexed) in the decoded string.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @param {number} k
18+
* @return {string}
19+
*/
20+
var decodeAtIndex = function(s, k) {
21+
let tapeLength = 0;
22+
let i = 0;
23+
24+
while (tapeLength < k) {
25+
if (isLetter(s[i])) {
26+
tapeLength++;
27+
} else {
28+
tapeLength *= parseInt(s[i]);
29+
}
30+
i++;
31+
}
32+
33+
while (i--) {
34+
if (isLetter(s[i])) {
35+
if (tapeLength === k) return s[i];
36+
tapeLength--;
37+
} else {
38+
tapeLength = Math.floor(tapeLength / parseInt(s[i]));
39+
k = k % tapeLength || tapeLength;
40+
}
41+
}
42+
};
43+
44+
function isLetter(char) {
45+
return /[a-z]/.test(char);
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.