-
Notifications
You must be signed in to change notification settings - Fork 2
/
m. Decode_String.cpp
60 lines (55 loc) · 1.28 KB
/
m. Decode_String.cpp
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
57
58
59
60
/*
Problem Link: https://practice.geeksforgeeks.org/problems/decode-the-string2444/1
Title: Decode the String
Difficulty: Easy
Author: Hariket Sukesh Kumar Sheth
Language: C++
*/
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
string decodedString(string s){
stack<char>st;
string ans;
for(int i=0;i<s.size();i++){
if(s[i]!=']') st.push(s[i]);
else{
string temp;
while(!st.empty() && st.top()!='['){
temp = st.top() + temp;
st.pop();
}
st.pop();
string num;
while (!st.empty() && isdigit(st.top())) {
num = st.top() + num;
st.pop();
}
int number = stoi(num);
string repeat;
for (int j = 0; j < number; j++)
repeat += temp;
for (char c : repeat)
st.push(c);
}
}
string res;
while (!st.empty()) {
res = st.top() + res;
st.pop();
}
return res;
}
};
int main(){
int t;
cin>>t;
while(t--){
string s;
cin>>s;
Solution ob;
cout<<ob.decodedString(s)<<"\n";
}
return 0;
}