-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecodeString.cpp
More file actions
56 lines (40 loc) · 2.64 KB
/
decodeString.cpp
File metadata and controls
56 lines (40 loc) · 2.64 KB
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
class Solution {
public:
string make(string& s, int& i){ // Here i and s are 'CALLED BY REFERENCE' ie. changing
// the value of i and s here will change the value of
// i and s present in 'decodeString()'
string res = "", k = ""; // 'res' will store the final result of this recursion/step
// and 'k' will store the string which was present in '[]'
// to be repetitively stored n times in res.
int n = 0; // No. of times k is present in res.
while(isdigit(s[i]) == true){ // Extracting the entire value present before '[' ie
// opening of square brackets to get number of times
n = n*10 + (s[i] - '0'); // k is to be repeated to store in res.
i++; // Ex 24[abc] where n will be 24.
}
for(i = i + 1; s[i] != ']'; i++){ //Iterating through string inside '[]' till ']'
// is encountered.
if(isdigit(s[i])) // If char encountered is a digit, we call make() again,
k += make(s, i); // and add the returned string to k. That's why, this is
// recursion i.e. function calling itself.
else // If char encountered is a character, add it to result as it
k += s[i]; // is.
}
for(int j = 0; j < n; j++) // Adding k 'n' times to 'res'
res += k;
return res; // Returning the resultant string for this recursion/step.
}
string decodeString(string s) {
string res = ""; // The result string, which will store the final result.
for(int i = 0; i < s.length(); i++){ // Loop to iterate through the string
// If a digit is present in the string, we know that the
if(isdigit(s[i])) // digit and the numbers that follows it must be the
res += make(s, i); // number of times the string present inside '[]' must be
// repeated. So we pass it to make() and it returns the
// result string formed after repeating the string inside.
else // If the character is an alphabet, simply add it to final
res += s[i]; // resultant string.
}
return res; // Return the resultant string.
}
};