Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string decodeString(string s) {
- string ans;
- stack<string> str;
- stack<int> count;
- int num = 0;
- for(char c: s){
- if(isdigit(c)){
- num = num*10 + c -'0';
- }
- else if(isalpha(c)){
- ans += c;
- }
- else if(c == '['){
- count.push(num);
- str.push(ans);
- ans = "";
- num = 0;
- }
- else if(c == ']'){
- string tmp = ans;
- int c = count.top()-1;
- count.pop();
- for(int i=0; i<c; i++)
- ans += tmp;
- ans = str.top()+ans;
- str.pop();
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement