Simple Text Editor

  • + 0 comments

    Java 15 Solution:-

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            int Q = Integer.parseInt(reader.readLine());
            StringBuilder S = new StringBuilder(); 
            Stack<String> stack = new Stack<>(); 
            while (Q-- > 0) {
                String[] input = reader.readLine().split(" ");
                int type = Integer.parseInt(input[0]);
    
                switch (type) {
                    case 1: 
                        stack.push(S.toString()); 
                        S.append(input[1]);
                        break;
    
                    case 2: 
                        int k = Integer.parseInt(input[1]);
                        stack.push(S.toString()); 
                        S.delete(S.length() - k, S.length());
                        break;
    
                    case 3: 
                        int index = Integer.parseInt(input[1]);
                        System.out.println(S.charAt(index - 1));
                        break;
    
                    case 4: 
                        if (!stack.isEmpty()) {
                            S = new StringBuilder(stack.pop());
                        }
                        break;
                }
            }
        }
    }