You are viewing a single comment's thread. Return to all comments →
js answer using solely regex to parse the input (and actually 2 'stacks'):
function processData(input) { let frontStack = new Array(); let tailStack = new Array(); let regex = /(?<=\n)(?<type>\d)\s*?(?<value>\d*)(?=(\n|$))/g; let inputObjArr = [...input.matchAll(regex)].map((result) => ( { 'type': Number(result.groups.type), 'value': result.groups.value } )); inputObjArr.forEach((inputObj) => { switch(inputObj.type){ case 1: { tailStack.push(inputObj.value); break; } case 2: { if(frontStack.length === 0){ while(tailStack.length > 0){ frontStack.push(tailStack.pop()); } } frontStack.pop(); break; } case 3: { if(frontStack.length === 0){ while(tailStack.length > 0){ frontStack.push(tailStack.pop()); } } console.log(frontStack.at(-1)) break; } } }) }
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
js answer using solely regex to parse the input (and actually 2 'stacks'):