We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My answer with Typescript, i create classes to easy resusable
classSEditor{private_stacks:string[];private_stacks_cursor:number;privategets(){returnthis._stacks[this._stacks_cursor]}/** * Simple editor with a [stacks] stored string state after every steps, * Can undo & redo with [cursor], [redo] will be overriden by [append]&[delete] */constructor(s:string,privatews:WriteStream){this._stacks=[s]this._stacks_cursor=0}public_append(w:string):void{this._stacks.splice(this._stacks_cursor+1)this._stacks.push(this.s+w)this._stacks_cursor++}public_delete(k:number):void{this._stacks.splice(this._stacks_cursor+1)this._stacks.push(this.s.substring(0,this.s.length-k))this._stacks_cursor++}public_print(k:number):void{this.ws.write(this.s[k-1]+'\n');}public_undo():void{this._move_cursor(-1)}public_redo():void{this._move_cursor(+1)}private_move_cursor(steps:number):void{this._stacks_cursor=Math.max(0,Math.min(this._stacks_cursor+steps,this._stacks.length-1));}}functionmain(){constws:WriteStream=createWriteStream(process.env['OUTPUT_PATH']);constq:number=parseInt(readLine().trim(),10);consts=newSEditor('',ws)for(leti=0;i<q;i++){let[t,w]=readLine().split(' ');switch(t){case'1':s._append(w);break;case'2':s._delete(parseInt(w,10));break;case'3':s._print(parseInt(w,10));break;case'4':s._undo();break;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
My answer with Typescript, i create classes to easy resusable