class Solution {
public String decodeString(String s) {
Stack<Pair<String,Integer>> stack= new Stack<>();
String temp="";
String ans="";
for(int i=0;i<s.length();)//ignore updation abhi pta nhi ki kaise increment krna hai
{
char c= s.charAt(i);
if(Character.isDigit©)// check whether c is digit or not
{
int num=0;
while(i<s.length() && Character.isDigit©)// for number greater than 9
{
num=num*10 +(c-‘a’);
i++;
c=s.charAt(i);
}
stack.push(new Pair(temp,num));//insert key and value
temp="";// reinistalize to empty
}
else if(Character.isLetter(c))
{
temp+=c;// add more key to temp
}
else if(c==']')
{
Pair<String,Integer> pair= stack.pop();// if bracket ends get top pair
int num= pair.getValue();
String key= pair.getKey();
for(int j=0;i<num;j++)
key+=temp;
if(stack.isEmpty())
{
ans+=key;
temp="";
}
else{
temp=key;//stack not emptythen it will be candidate to other keyvalue pair
}
}
i++;
}
ans+=temp;
return ans;
}
}