File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 690
690
877|[ Stone Game] ( ./0877-stone-game.js ) |Medium|
691
691
878|[ Nth Magical Number] ( ./0878-nth-magical-number.js ) |Hard|
692
692
879|[ Profitable Schemes] ( ./0879-profitable-schemes.js ) |Hard|
693
+ 880|[ Decoded String at Index] ( ./0880-decoded-string-at-index.js ) |Medium|
693
694
884|[ Uncommon Words from Two Sentences] ( ./0884-uncommon-words-from-two-sentences.js ) |Easy|
694
695
889|[ Construct Binary Tree from Preorder and Postorder Traversal] ( ./0889-construct-binary-tree-from-preorder-and-postorder-traversal.js ) |Medium|
695
696
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments