Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string fun (int &idx, string &str)
- {
- string res = "";
- string decoded_string = "";
- while (idx < str.size())
- {
- if (isdigit(str[idx]))
- {
- string num ="";
- while ( idx < str.size() && isdigit(str[idx] ) )
- num += str[idx++];
- int t = stoi (num);
- idx++;
- decoded_string = fun (idx,str);
- for (int ttimes = 1 ; ttimes <= t ; ttimes++)
- res += decoded_string;
- }
- else if (isalpha(str[idx]) )
- res+= str[idx];
- else if (str[idx] == ']')
- return res;
- idx++;
- }
- return res;
- }
- string decodeString(string s) {
- int idx = 0 ;
- return fun (idx ,s);
- }
- };
- /*
- 1 [ 3 [ a 2 [ c ] ] 2 [ a b c ] 3 [ c d ] e f ] c
- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
- ^
- "accaccaccabcabccdcdcdefc"
- s i
- i find a number n
- [
- decoded string = (i,s)
- ]
- while (n--)
- res += decoded_string
- accaccaccabcabccdcdcdef
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement