I need to decode a string recursively encoded as count followed by substring
An encoded string (s) is given, the task is to decode it. The pattern in which the strings are encoded is as follows.
Examples:
Input : str[] = "1[b]" Output : b
Input : str[] = "2[ab] Output : abab
Input : str[] = "2[a2[b]]" Output : abbabb
Input : str[] = "3[b2[ca]]" Output : bcacabcacabcaca
Below is the code I tried to achieve the same. All I know is it can be solved using two stacks.
public class Main {
public static void main(String[] args) {
Stack<Interger> s1 = new Stack();
Stack<String> s2 = new Stack();
String result = "";
for(int i = 0; i < args.length; i++){
if(Interger.parseInt(args[i]) == 0){
s1.push(args[i]);
}
if(args[i] == 0){
if(args[i] == ']'){
result = s2.pop();
}
if(args[i] == '['){
continue;
}
s2.push(args[i])
}
}
}
}
Can anyone help me what is the efficient way to write code in order to get the expected output?