function
decode(str) {
let integerstack = [];
let stringstack = [];
let temp =
""
,
result =
""
;
for
(let i = 0; i < str.length; i++) {
let count = 0;
if
(str[i] >=
"0"
&& str[i] <=
"9"
) {
while
(str[i] >=
"0"
&& str[i] <=
"9"
) {
count = count * 10 + str[i] -
"0"
;
i++;
}
i--;
integerstack.push(count);
}
else
if
(str[i] ==
"]"
) {
temp =
""
;
count = 0;
if
(integerstack.length > 0) {
count =
integerstack[integerstack.length - 1];
integerstack.pop();
}
while
(
stringstack.length > 0 &&
stringstack[stringstack.length - 1] !=
"["
) {
temp =
stringstack[stringstack.length - 1] +
temp;
stringstack.pop();
}
if
(
stringstack.length > 0 &&
stringstack[stringstack.length - 1] ==
"["
) {
stringstack.pop();
}
for
(let j = 0; j < count; j++) {
result = result + temp;
}
for
(let j = 0; j < result.length; j++) {
stringstack.push(result[j]);
}
result =
""
;
}
else
if
(str[i] ==
"["
) {
if
(str[i - 1] >=
"0"
&& str[i - 1] <=
"9"
) {
stringstack.push(str[i]);
}
else
{
stringstack.push(str[i]);
integerstack.push(1);
}
}
else
{
stringstack.push(str[i]);
}
}
while
(stringstack.length > 0) {
result =
stringstack[stringstack.length - 1] + result;
stringstack.pop();
}
return
result;
}
let str =
"3[f2[jk]]"
;
console.log(decode(str));