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.
static StringBuilder res = new StringBuilder();
static StackstringStack = new Stack<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int q = scanner.nextInt();
//number of quiries
while( (q--) > 0 ){
int typeOfOp= scanner.nextInt();
switch (typeOfOp){
case 1:
String stringToAppend = scanner.next();
append(stringToAppend);
break;
case 2:
int k = scanner.nextInt();
deleteLast_K_char(k);
break;
case 3:
int kToPrint = scanner.nextInt();
printKchar(kToPrint);
break;
case 4:
undo();
}
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
public static void append(String s){
stringStack.push(res.toString());
res.append(s);
}
public static void deleteLast_K_char(int k){
stringStack.push(res.toString());
//System.out.println("before delete ----> " + res);
res.delete(res.length() - k, res.length());
//System.out.println("after delete ----> "+ res);
}
public static void printKchar(int k){
System.out.println(res.charAt(k-1));
}
public static void undo(){
res=new StringBuilder(stringStack.pop());
}
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 →
Using stack DS
static StringBuilder res = new StringBuilder(); static StackstringStack = new Stack<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt();