Simple Text Editor

Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Java O(1)

    public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    
            int Q = Integer.parseInt(bufferedReader.readLine().trim());
            StringBuilder S = new StringBuilder();
            Stack<String> history = new Stack<>();
    
            for (int i = 0; i < Q; i++) {
                String[] inputs = bufferedReader.readLine().split(" ");
                int type = Integer.parseInt(inputs[0]);
    
                switch (type) {
                    case 1: // Append(W)
                        history.push(S.toString());
                        S.append(inputs[1]);
                        break;
                    case 2: // Delete(k)
                        int k = Integer.parseInt(inputs[1]);
                        history.push(S.toString());
                        S.delete(S.length() - k, S.length());
                        break;
                    case 3: // Print(k)
                        int index = Integer.parseInt(inputs[1]) - 1;
                        bufferedWriter.write(S.charAt(index));
                        bufferedWriter.newLine();
                        break;
                    case 4: // Undo
                        S = new StringBuilder(history.pop());
                        break;
                }
            }
    
            bufferedReader.close();
            bufferedWriter.flush();
            bufferedWriter.close();
        }
    
  • + 0 comments

    JS

    function processData(input) {
        const undo = [""];
        const operations = [
            (append) => undo.push(undo.at(-1) + append),
            (_delete) => undo.push(undo.at(-1).slice(0, undo.at(-1).length - _delete)),
            (print) => console.log(undo.at(-1).at(print - 1)),
            () => undo.pop()
        ];
        input.split("\n").slice(1).forEach((operation) => operations[operation.split(" ")[0] - 1](operation.split(" ")[1]));
    }
    
  • + 0 comments

    java 8

    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 input = new Scanner(System.in);
            int q = input.nextInt();
            
            Stack<String> stack = new Stack<>();
            StringBuilder sb = new StringBuilder();
            
            for(int j = 0; j < q; j++) {
                int choose = input.nextInt();
                switch(choose) {
                    case 1:
                        stack.push(sb.toString());
                        String newText = input.next();
                        sb.append(newText);
                        break;
                    case 2:
                        stack.push(sb.toString());
                        int k = input.nextInt();
                        sb.delete(sb.length() - k, sb.length());
                        break;
                    case 3:
                        int kth = input.nextInt();
                        System.out.println(sb.charAt(kth - 1));
                        break;
                    case 4:
                        sb.setLength(0);
                        sb.append(stack.pop());
                        break;
                }
            }
        }
    
  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    
    class Operation {
        int type;
        String value;
        public Operation(int type, String value) {
            this.type = type;
            this.value = value;
        }
    }
    
    public class Solution {
        
        private static StringBuilder text = new StringBuilder("");
        
        // trace append and delete operations
        private static Stack<Operation> trace = new Stack<>(); 
        
        public static void main(String[] args) {
            
            Scanner scan = new Scanner(System.in);
            int q = scan.nextInt();
            for(int i = 0; i < q; i++) {
            int operation = scan.nextInt();
           
                switch(operation) {
                    case 1:
                    String newText = scan.next();
                    trace.push(new Operation(1, newText));
                    add(newText);
                    break;
                    
                    case 2:
                    int numToDelete = scan.nextInt();
                    int textLen = text.length();
                    trace.push(new Operation(2, text.substring(textLen - numToDelete, textLen)));
                    delete(numToDelete);
                    break;
                    
                    case 3:
                    int indextToPrint = scan.nextInt();
                    System.out.println(text.charAt(indextToPrint-1));
                    break;
     
                    case 4:
                    undo();
                    break;
                }
            }
            scan.close();
        }
        
        
        private static void delete(int numToDelete) {
            int textLen = text.length();
            String tempText = text.substring(0, textLen - numToDelete);
            text.setLength(0);
            text.append(tempText);
        }
        
        private static void add(String newText) {
            text.append(newText);
        }
        
        private static void undo() {
            if(!trace.isEmpty()) {
                Operation lastOperation = trace.pop();
                if(lastOperation.type == 1) {
                    delete(lastOperation.value.length());
                } else {
                    add(lastOperation.value);
                }
            }
        }
    }
    
  • + 0 comments
    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 input = new Scanner(System.in);
            StringBuffer sb = new StringBuffer("");
            int q = input.nextInt();
            Stack<String> deletedString = new Stack<>();
            Stack<String> appendedString = new Stack<>();
            Stack<Integer> trace = new Stack<>();
            
            for(int j = 0; j < q; j++){
                int choose = input.nextInt();
                switch(choose){
                    case 1:
                        String str = input.next();
                        sb.append(str);
                        appendedString.push(str);
                        trace.push(1);
                        break;
                    case 2:
                        int k = input.nextInt();
                        int startIndex = Math.max(0, sb.length() - k);
                        deletedString.push(sb.substring(startIndex));
                        sb.delete(startIndex, sb.length());
                        trace.push(2);
                        break;
                    case 3:
                        int i = input.nextInt();
                        if(i > 0 && i <= sb.length()){
                            System.out.println(sb.charAt(i - 1));
                        }
                        break;
                    case 4:
                        if(!trace.isEmpty()){
                            int operate = trace.pop();
                            if(operate == 1){
                                String string = appendedString.pop();
                                sb.delete(sb.length() - string.length(), sb.length());
                            }
                            else if(operate == 2){
                                String string2 = deletedString.pop();
                                sb.append(string2);
                            }
                        }
                        
                        break;
                        
                }
            }
            input.close();
        }
    }