package algorithms.simpletexteditor;

import java.io.FileNotFoundException;
import java.util.*;

public class Solution {

  private static StringBuilder result;
  private static StringBuilder sb;

  public static void main(String[] args) throws FileNotFoundException {
  Scanner scanner = new Scanner(System.in);
//    Scanner scanner = new Scanner(new FileInputStream("/home/webdude/Documents/Projects/HackerRank/input10.txt"));
    int linesOfInput = Integer.parseInt(scanner.nextLine());
    List<List<String>> commands = new ArrayList<>();

    for (int i = 0; i < linesOfInput; i++) {
      String[] commandArgs = scanner.nextLine().split("\\s+");
      if (commandArgs.length > 1) {
        commands.add(Arrays.asList(commandArgs[0], commandArgs[1]));
      } else {
        commands.add(Collections.singletonList(commandArgs[0]));
      }
    }

    printResult(executeCommands(commands));
  }

  private static void printResult(char[] result) {
    sb = new StringBuilder();

    for (char character : result) {
      sb.append(character);
      sb.append(System.lineSeparator());
    }

    System.out.println(sb.toString());
  }

  public static char[] executeCommands(List<List<String>> input) {
    result = new StringBuilder();

    Deque<Command> commandQueue = new LinkedList<>();

    for (List<String> strings : input) {
      Command command = parseCommand(strings);
      switch (command.getCommandCode()) {
        case Command.APPEND:
        case Command.DELETE:
          commandQueue.add(command);
          break;
        case Command.UNDO:
          commandQueue.pollLast();
          break;
        case Command.PRINT:
          renderState(commandQueue);
          print(command.commandArgument);
          break;
      }
    }

    return result.toString().toCharArray();
  }

  private static void renderState(Queue<Command> commandQueue) {
    sb = new StringBuilder();

    commandQueue.forEach(command -> {
      if (command.getCommandCode().equals(Command.APPEND)) {
        append(command.commandArgument);
      } else if (command.getCommandCode().equals(Command.DELETE)) {
        delete(command.commandArgument);
      }
    });
  }

  private static void append(String commandArgument) {
    sb.append(commandArgument);
  }

  private static void delete(String commandArgument) {
    sb.delete(sb.length() - Integer.parseInt(commandArgument), sb.length());
  }

  private static void print(String charToPrint) {
    result.append(sb.charAt(Integer.parseInt(charToPrint) - 1));
  }

  private static Command parseCommand(List<String> input) {
    return new Command(input.get(0), input.size() > 1 ? input.get(1) : null);
  }

  static class Command {
    static final String APPEND = "1";
    static final String DELETE = "2";
    static final String PRINT = "3";
    static final String UNDO = "4";

    private final String commandArgument;
    private final String commandCode;

    public Command(String commandCode, String commandArgument) {
      this.commandCode = commandCode;
      this.commandArgument = commandArgument;
    }

    public String getCommandCode() {
      return this.commandCode;
    }
  }
}

@@ -35,46 +35,8 @@
 r;%0A%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -197,39 +197,12 @@
 gs) 
-throws FileNotFoundException 
 %7B%0A
+  
   Sc
@@ -245,125 +245,8 @@
 n);%0A
-//    Scanner scanner = new Scanner(new FileInputStream(%22/home/webdude/Documents/Projects/HackerRank/input10.txt%22));%0A
     
@@ -1009,21 +1009,24 @@
 tring%3E%3E 
-input
+commands
 ) %7B%0A    
@@ -1066,36 +1066,28 @@
   Deque%3C
-Command%3E commandQueu
+String%3E stat
 e = new 
@@ -1095,32 +1095,53 @@
 inkedList%3C%3E();%0A%0A
+    state.push(%22%22);%0A%0A
     for (List%3CSt
@@ -1156,21 +1156,24 @@
 rings : 
-input
+commands
 ) %7B%0A    
@@ -1329,32 +1329,53 @@
     
-commandQueue.add(command
+state.push(renderState(command, state.peek())
 );%0A 
@@ -1431,29 +1431,17 @@
     
-commandQueue.pollLast
+state.pop
 ();%0A
@@ -1499,51 +1499,52 @@
     
-renderState(commandQueue);%0A          print(
+print(Objects.requireNonNull(state.peek()), 
 comm
@@ -1663,20 +1663,22 @@
  static 
-void
+String
  renderS
@@ -1686,30 +1686,23 @@
 ate(
-Queue%3C
 Command
-%3E
  command
 Queu
@@ -1697,20 +1697,36 @@
  command
-Queu
+, String currentStat
 e) %7B%0A   
@@ -1753,72 +1753,150 @@
 der(
-);%0A%0A    commandQueue.forEach(command -%3E %7B%0A      if (command.getC
+currentState);%0A%0A    final String commandCode = command.getCommandCode();%0A    final String commandArgument = command.commandArgument;%0A    if (c
 omma
@@ -1893,34 +1893,32 @@
  if (commandCode
-()
 .equals(Command.
@@ -1938,25 +1938,15 @@
     
-  
 append(
-command.
 comm
@@ -1955,26 +1955,24 @@
 dArgument);%0A
-  
     %7D else i
@@ -1981,33 +1981,20 @@
 (command
-.getCommand
 Code
-()
 .equals(
@@ -2022,25 +2022,15 @@
     
-  
 delete(
-command.
 comm
@@ -2047,25 +2047,42 @@
 t);%0A    
-  
 %7D
+%0A
 %0A    
-%7D
+return sb.toString(
 );%0A  %7D%0A%0A
@@ -2345,16 +2345,37 @@
 (String 
+currentState, String 
 charToPr
@@ -2399,18 +2399,28 @@
 .append(
-sb
+currentState
 .charAt(

optimize the text editor a bit so more tests pass