Decode Bracket String
JavaScript
Hard
4 views
Problem Description
Input like 3[a2[c]] means repeat inside brackets. Decode and print final string.
Input Format
One line encoded string.
Output Format
Decoded string.
Constraints
Decoded length will be
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();let numSt=[];let strSt=[];let cur='';let num=0;for(const ch of s){const code=ch.charCodeAt(0);if(code>=48&&code<=57){num=num*10+(code-48);}else if(ch==='['){numSt.push(num);strSt.push(cur);num=0;cur='';}else if(ch===']'){const k=numSt.pop();const prev=strSt.pop();cur=prev+cur.repeat(k);}else cur+=ch;}process.stdout.write(cur);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!