-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path880-Decoded-String-at-Index.cpp
71 lines (66 loc) · 2.21 KB
/
880-Decoded-String-at-Index.cpp
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
69
70
71
// Author : Vicen-te
// Date : 09/28/2023 (MM-DD-YYYY)
/*
* Description:
* 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.
*
* Ex.
* Input: s = "leet2code3", k = 10
* Output: "o"
* Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode".
* The 10th letter in the string is "o".
*
* Algorithm:
* 1. Calculate the length by adding the characters of the string and multiplying them by
* the digit characters.
* 2. Iterate through the string in reverse and check if the current character is a digit.
* 3. If it is, remove repeated words by dividing the length with the digit and adjust
* the character's position(k) using the modulus operator(giving us the remainder).
* 4. If it's not a digit, check if the character's position(k) is equal to the length or 0
* (this will indicate if it has reached the desired position) and return the desierd character
* if its true. If not, subtract one from the length.
*
* Time Complexity: O(N)
* Space Complexity: O(1)
*/
class Solution {
public:
string decodeAtIndex(string s, int k) {
long length = 0;
uint8_t i = 0;
while(i < s.length())
{
if(isdigit(s[i]))
{
length *= (s[i] - '0');
}
else
{
++length;
}
++i;
}
for (uint8_t j = i - 1; j >= 0; --j)
{
if (isdigit(s[j]))
{
length /= (s[j] - '0');
k %= length;
}
else
{
if(k == length || k == 0)
return string(1, s[j]);
--length;
}
}
return "";
}
};