JSFiddle - React, Tailwind, and code Playground

by Prathameshsb

JavaScript

const decodeString = (s) => {
	let index = 0;
  function decode() {
  	let result = '';
    
    while(index < s.length && s[index] !== ']') {
    	if(!isNaN(s[index])) {
      	let num = 0;
        while(!isNaN(s[index])) {
        	num = num * 10 + parseInt(s[index]);
          index++;
        }
        index++;
        const innerVal = decode();
        index++;
        result += innerVal.repeat(num);
      }else {
      	result += s[index];
        index++
      }
    }
    
    return result;
  }
  return decode();
};

console.log(decodeString("3[a]2[bc]"))