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.
publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerinput=newScanner(System.in);StringBuffersb=newStringBuffer("");intq=input.nextInt();Stack<String>deletedString=newStack<>();Stack<String>appendedString=newStack<>();Stack<Integer>trace=newStack<>();for(intj=0;j<q;j++){intchoose=input.nextInt();switch(choose){case1:Stringstr=input.next();sb.append(str);appendedString.push(str);trace.push(1);break;case2:intk=input.nextInt();intstartIndex=Math.max(0,sb.length()-k);deletedString.push(sb.substring(startIndex));sb.delete(startIndex,sb.length());trace.push(2);break;case3:inti=input.nextInt();if(i>0&&i<=sb.length()){System.out.println(sb.charAt(i-1));}break;case4:if(!trace.isEmpty()){intoperate=trace.pop();if(operate==1){Stringstring=appendedString.pop();sb.delete(sb.length()-string.length(),sb.length());}elseif(operate==2){Stringstring2=deletedString.pop();sb.append(string2);}}break;}}input.close();}}
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 →