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.
I tryied to make a readable one, not actually that short, but still very efficient:
publicclassSolution{/* utilities */staticScannerin=newScanner(System.in);staticvoidprintln(Stringmsg,Object...params){System.out.println(String.format(msg,params));}staticvoidprintln(charch){System.out.println(ch);}/* main */staticfinalintADD=1,DEL=2,PRINT=3,UNDO=4;staticfinalDeque<String>lastStates=newArrayDeque<>();staticStringcontent="";publicstaticvoidmain(String[]args){intq=in.nextInt();for(inti=0;i<q;i++){switch(in.nextInt()){caseADD:add();break;caseDEL:del();break;casePRINT:print();break;caseUNDO:undo();break;}}}staticvoidadd(){lastStates.push(content);content+=in.next();}staticvoiddel(){lastStates.push(content);intn=in.nextInt();content=content.substring(0,content.length()-n);}staticvoidprint(){intn=in.nextInt();println(content.charAt(n-1));}staticvoidundo(){content=lastStates.poll();}}
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 →
I tryied to make a readable one, not actually that short, but still very efficient: