Advertisement
SalmaYasser

Untitled

Nov 10th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4. string fun (int &idx, string &str)
  5. {
  6. string res = "";
  7. string decoded_string = "";
  8.  
  9. while (idx < str.size())
  10. {
  11.  
  12. if (isdigit(str[idx]))
  13. {
  14.  
  15. string num ="";
  16. while ( idx < str.size() && isdigit(str[idx] ) )
  17. num += str[idx++];
  18. int t = stoi (num);
  19.  
  20. idx++;
  21. decoded_string = fun (idx,str);
  22.  
  23. for (int ttimes = 1 ; ttimes <= t ; ttimes++)
  24. res += decoded_string;
  25.  
  26. }
  27. else if (isalpha(str[idx]) )
  28. res+= str[idx];
  29.  
  30. else if (str[idx] == ']')
  31. return res;
  32.  
  33. idx++;
  34.  
  35. }
  36. return res;
  37. }
  38. string decodeString(string s) {
  39. int idx = 0 ;
  40. return fun (idx ,s);
  41.  
  42.  
  43. }
  44. };
  45.  
  46. /*
  47.  
  48.  
  49. 1 [ 3 [ a 2 [ c ] ] 2 [ a b c ] 3 [ c d ] e f ] c
  50. 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
  51. ^
  52. "accaccaccabcabccdcdcdefc"
  53. s i
  54. i find a number n
  55. [
  56. decoded string = (i,s)
  57. ]
  58.  
  59. while (n--)
  60. res += decoded_string
  61.  
  62. accaccaccabcabccdcdcdef
  63.  
  64.  
  65.  
  66. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement