Advertisement
nikunjsoni

394

Mar 29th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string decodeString(string s) {
  4.         string ans;
  5.         stack<string> str;
  6.         stack<int> count;
  7.         int num = 0;
  8.         for(char c: s){
  9.             if(isdigit(c)){
  10.                 num = num*10 + c -'0';
  11.             }
  12.             else if(isalpha(c)){
  13.                 ans += c;
  14.             }
  15.             else if(c == '['){
  16.                 count.push(num);
  17.                 str.push(ans);
  18.                 ans = "";
  19.                 num = 0;
  20.             }
  21.             else if(c == ']'){
  22.                 string tmp = ans;
  23.                 int c = count.top()-1;
  24.                 count.pop();
  25.                 for(int i=0; i<c; i++)
  26.                     ans += tmp;
  27.                 ans = str.top()+ans;
  28.                 str.pop();
  29.             }
  30.         }
  31.         return ans;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement