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.
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int Q = sc.nextInt();
ArrayList <String> operations = new ArrayList<>();
for(int i=0;i<=Q;i++)
{
operations.add(sc.nextLine());
}
operations.remove(0);
String s = new String();
Stack<String> stack = new Stack<>();
stack.push(s);
for(int i=0;i<Q;i++)
{
String temp = operations.get(i);
String arr[] = temp.split(" ");
switch(arr[0])
{
case "1":
{
s = s+arr[1];
stack.push(s);
break;
}
case "2":
{
s = s.substring(0,s.length()-Integer.parseInt(arr[1]));
stack.push(s);
break;
}
case "3":
{
int k = Integer.parseInt(arr[1]);
System.out.println(s.charAt(k-1));
break;
}
case "4":
{
stack.pop();
s = stack.peek();
}
}
}
}
}
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 →
import java.io.; import java.util.;
public class Solution {
}