Skip to content

Commit fc9ae0c

Browse files
committed
day 20
1 parent 03d12d1 commit fc9ae0c

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Decoded String at Index
3+
=======================
4+
5+
An encoded string S is given. To find and write the decoded string to a tape, the encoded string is read one character at a time and the following steps are taken:
6+
7+
If the character read is a letter, that letter is written onto the tape.
8+
If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total.
9+
Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string.
10+
11+
Example 1:
12+
Input: S = "leet2code3", K = 10
13+
Output: "o"
14+
Explanation:
15+
The decoded string is "leetleetcodeleetleetcodeleetleetcode".
16+
The 10th letter in the string is "o".
17+
18+
Example 2:
19+
Input: S = "ha22", K = 5
20+
Output: "h"
21+
Explanation:
22+
The decoded string is "hahahaha". The 5th letter is "h".
23+
24+
Example 3:
25+
Input: S = "a2345678999999999999999", K = 1
26+
Output: "a"
27+
Explanation:
28+
The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
29+
30+
Constraints:
31+
2 <= S.length <= 100
32+
S will only contain lowercase letters and digits 2 through 9.
33+
S starts with a letter.
34+
1 <= K <= 10^9
35+
It's guaranteed that K is less than or equal to the length of the decoded string.
36+
The decoded string is guaranteed to have less than 2^63 letters.
37+
*/
38+
39+
class Solution
40+
{
41+
public:
42+
string decodeAtIndex(string S, int K)
43+
{
44+
long size = 0;
45+
int N = S.size();
46+
47+
for (int i = 0; i < N; ++i)
48+
{
49+
if (isdigit(S[i]))
50+
size *= S[i] - '0';
51+
else
52+
size++;
53+
}
54+
55+
for (int i = N - 1; i >= 0; --i)
56+
{
57+
K %= size;
58+
if (K == 0 && isalpha(S[i]))
59+
return (string) "" + S[i];
60+
61+
if (isdigit(S[i]))
62+
size /= S[i] - '0';
63+
else
64+
size--;
65+
}
66+
return "";
67+
}
68+
};

Leetcode Daily Challenge/December-2020/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
| 17. | [4Sum II](https://leetcode.com/explore/challenge/card/december-leetcoding-challenge/571/week-3-december-15th-december-21st/3569/) | [cpp](./17.%204Sum%20II.cpp) |
2222
| 18. | [Increasing Triplet Subsequence](https://leetcode.com/explore/challenge/card/december-leetcoding-challenge/571/week-3-december-15th-december-21st/3570/) | [cpp](./18.%20Increasing%20Triplet%20Subsequence.cpp) |
2323
| 19. | [Cherry Pickup II](https://leetcode.com/explore/challenge/card/december-leetcoding-challenge/571/week-3-december-15th-december-21st/3571/) | [cpp](./19.%20Cherry%20Pickup%20II.cpp) |
24-
| 20. | []() |[cpp](./20.%20.cpp) |
24+
| 20. | [Decoded String at Index](https://leetcode.com/explore/challenge/card/december-leetcoding-challenge/571/week-3-december-15th-december-21st/3572/) |[cpp](./20.%20Decoded%20String%20at%20Index.cpp) |
2525
| 21. | [Smallest Range II](https://leetcode.com/explore/challenge/card/december-leetcoding-challenge/571/week-3-december-15th-december-21st/3573/) |[cpp](./21.%20Smallest%20Range%20II.cpp) |

0 commit comments

Comments
 (0)