using System.Collections.Generic;
public static void Main()
string inputString= "3[a]2[bc]";
List<int> openIndexes=new List<int>();
Console.WriteLine("inputString "+inputString);
while(inputString.Contains('['))
inputString = formatString(inputString,openIndexes);
Console.WriteLine("outString "+inputString);
public static string formatString(string inputString,List<int> openIndexes)
int openIndex,closeIndex;
for(int i=0;i<inputString.Length;i++)
else if(inputString[i]==']')
Console.WriteLine("Wrong Format");
openIndex=openIndexes.Last();
string repeatString = inputString.Substring(openIndex+1,closeIndex-(openIndex+1));
int repeatNumber=Convert.ToInt32( inputString.Substring(openIndex-1,1));
string newReplaceString="";
string replaceString=inputString.Substring(openIndex-1,closeIndex-(openIndex-2));
for(int j=0;j<repeatNumber;j++)
newReplaceString=newReplaceString+repeatString;
inputString=inputString.Replace(replaceString,newReplaceString);
openIndexes.Remove(openIndex);