Skip to content

Instantly share code, notes, and snippets.

@aswinmprabhu
Created March 16, 2019 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aswinmprabhu/7a9f19452d50907047e404d75f2cab32 to your computer and use it in GitHub Desktop.
Save aswinmprabhu/7a9f19452d50907047e404d75f2cab32 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
string nstr;
string decode(string str, int *i)
{
string retval;
while (*i < str.length() && str[*i] != ']')
{
if (!isdigit(str[*i]))
{
if (str[*i] == '[')
{
int n = stoi(nstr);
nstr = "";
*i += 1;
string istr = decode(str, i);
*i += 1;
while (n-- > 0)
{
retval += istr;
}
}
else
retval += str[(*i)++];
}
else
{
nstr += str[(*i)++];
}
}
return retval;
}
int main()
{
string str;
cout << "Enter the string to be decoded : ";
cin >> str;
int i = 0;
string ans = decode(str, &i);
cout << ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment