You are viewing a single comment's thread. Return to all comments →
Java Solution
import java.io.*; import java.util.*; public class Solution { private static void solve (List<String> operations) { Stack<String> notepad = new Stack<>(); notepad.push(""); for (String operation: operations) { Integer command = Integer.valueOf(operation.split(" ")[0]); if (command == 1) { notepad.push(notepad.peek() + operation.split(" ")[1]); } else if (command == 2) { Integer n = Integer.valueOf(operation.split(" ")[1]); String original = notepad.peek(); notepad.push(original.substring(0, (original.length() - n))); } else if (command == 3) { Integer n = Integer.valueOf(operation.split(" ")[1]); System.out.println(notepad.peek().charAt(n - 1)); } else if (command == 4) { notepad.pop(); } } } public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); List < String > operations = new ArrayList <> (); Integer q = Integer.valueOf(br.readLine());; for (int i = 0; i < q; i++) operations.add(br.readLine()); solve(operations);; } catch (IOException ex) { System.out.println(ex.getMessage()); } } }
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