Untitled
unknown
plain_text
2 years ago
1.1 kB
9
Indexable
class Solution {
public:
string decodeString(string s) {
stack<char>st;
for(int i=0;i<s.size();i++){
if(s[i]!=']') st.push(s[i]);
else if(s[i]==']'){
string encoded_string=""; //get the encoded_string in tmp
while(st.top()!='['){
encoded_string=st.top()+encoded_string;
st.pop();
}
st.pop(); //removes the [
string count=""; //get the k in count
while(!st.empty() && isdigit(st.top())){
count= st.top() +count;
st.pop();
}
int k=stoi(count);
string tmp2="";
while(k--){
tmp2=tmp2+encoded_string; // tmp2 is k X encoded_string
}
for(char ch:tmp2) st.push(ch);
}
}
string res="";
while(!st.empty()){
res=st.top()+res;
st.pop();
}
return res;
}
};Editor is loading...
Leave a Comment