You are viewing a single comment's thread. Return to all comments →
Java Solution
public class Solution {
public static void main(String[] args) { Scanner sc; sc = new Scanner(System.in); try{ int inputOp=0; int opsCount = sc.nextInt(); String value = ""; String finalValue=""; Stack<String> stack = new Stack<>(); for (int i = 0; i < opsCount; i++) { inputOp=sc.nextInt(); if(inputOp==1){ value=sc.next(); stack.push(finalValue); finalValue=finalValue+value; }else if (inputOp==2){ value=sc.next(); stack.push(finalValue); finalValue=finalValue.substring(0,(finalValue.length()-Integer.parseInt(value))); }else if (inputOp==3){ value=sc.next(); System.out.println(finalValue.charAt(Integer.parseInt(value)-1)); }else if (inputOp==4){ finalValue=stack.pop(); } } }catch(Exception e){ System.out.println("Error : "+e); }finally{ sc.close(); } } }
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 →
Java Solution
public class Solution {