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){Scannerscanner=newScanner(System.in);intqueries=scanner.nextInt();Stack<String>stack=newStack<>();StringBuilderbuilder=newStringBuilder();scanner.nextLine();for(inti=0;i<queries;i++){Stringquery=scanner.nextLine();intoperation=Integer.parseInt(query.split(" ")[0]);switch(operation){case1://appendstack.push(builder.toString());builder.append(query.split(" ")[1]);break;case2://delete last K charsstack.push(builder.toString());builder.delete(builder.length()-Integer.parseInt(query.split(" ")[1]),builder.length());break;case3://print kth characterSystem.out.println(builder.charAt(Integer.parseInt(query.split(" ")[1])-1));break;case4://undobuilder=newStringBuilder(stack.pop());break;}}scanner.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 →