-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDecode_Strings.txt
60 lines (59 loc) · 1.33 KB
/
Decode_Strings.txt
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
class Solution {
public:
string decodeString(string s)
{
stack<char> ss;
int val=0;
for(int i=0;i<s.length();i++)
{
if(s[i]==']')
{
string a="";
while(ss.top()!='[')
{
a+=ss.top();
ss.pop();
}
reverse(a.begin(),a.end());
ss.pop();
string num="";
while(!ss.empty() && ss.top()>='0' && ss.top()<='9')
{
num+=(ss.top());
ss.pop();
}
reverse(num.begin(),num.end());
// int count=1;
// val=0;
// for(int q=0;q<num.length();q++)
// {
// val=val*count+(num[q]-'0');
// count*=10;
// }
int val=stoi(num);
cout<<val<<" ";
string ans="";
int m=0;
while(m<val)
{
ans.append(a);
m++;
}
for(int j=0;j<ans.length();j++)
{
ss.push(ans[j]);
}
}
else
ss.push(s[i]);
}
string n="";
while(!ss.empty())
{
n+=ss.top();
ss.pop();
}
reverse(n.begin(),n.end());
return n;
}
};