Sort by

recency

|

539 Discussions

|

  • + 0 comments
    def TextEditor(ops):
        s=''
        done=[]
        for i in ops:
            if i[0]=='1':
                s=s+(i[1])
                done.append(i)
            elif i[0]=='2':
                e=s[(len(s)-int(i[1])):len(s)]
                s=s[0:(len(s)-int(i[1]))]
                i.append(e)
                done.append(i)
            elif i[0]=='3':
                print(s[int(i[1])-1])
            elif i[0]=='4':
                u=done[-1]
                if u[0]=='1':
                    s=s[0:(len(s)-len(u[1]))]
                elif u[0]=='2':
                    s=s+(u[2])
                done.pop()
        return
        
        
    q=int(input()) 
    ops=[]
    for i in range(q):
        ops.append(input().split())
    TextEditor(ops)
    
  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    int main() { string S1 = ""; int Queries = 0; cin >> Queries; int Operations = 0; string S2 = ""; stack Temp; while (Queries--) { cin >> Operations; if (Operations == 1) { Temp.push(S1); cin >> S2; S1 += S2; } else if (Operations == 2) { Temp.push(S1); cin >> Operations; S1.erase(S1.size() - Operations, S1.size()); } else if (Operations == 3) { cin >> Operations; cout << S1[Operations-1] << endl;; } else if (Operations == 4) { S1 = Temp.top(); Temp.pop(); } } }

  • + 0 comments

    For Java 7. Ok. You need to know that i generated this by chatGPT, tell him to optimize my oringinal. Eventhrough it's still not good enough, but try a few times then it might pass.

    import java.util.Scanner; import java.util.Stack;

    public class SolutionSolution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); scanner.nextLine();

        StringBuilder currentText = new StringBuilder();
        Stack<Operation> history = new Stack<>();
    
        for (int i = 0; i < q; i++) {
            int type = scanner.nextInt();
    
            switch (type) {
                case 1: // Append
                    String w = scanner.next();
                    history.push(new Operation(1, w.length()));
                    currentText.append(w);
                    break;
    
                case 2: // Delete
                    int k = scanner.nextInt();
                    String deletedText = currentText.substring(currentText.length() - k);
                    history.push(new Operation(2, deletedText));
                    currentText.delete(currentText.length() - k, currentText.length());
                    break;
    
                case 3: // Print
                    int pos = scanner.nextInt();
                    System.out.println(currentText.charAt(pos - 1));
                    break;
    
                case 4: // Undo
                    if (!history.isEmpty()) {
                        Operation lastOperation = history.pop();
                        if (lastOperation.type == 1) {
                            // Undo append: remove the last 'length' characters
                            currentText.delete(currentText.length() - lastOperation.length, currentText.length());
                        } else if (lastOperation.type == 2) {
                            // Undo delete: append the deleted string
                            currentText.append(lastOperation.data);
                        }
                    }
                    break;
    
                default:
                    break;
            }
        }
    
        scanner.close();
    }
    

    static class Operation { int type; // 1 for append, 2 for delete int length; String data;

        Operation(int type, int length) {
            this.type = type;
            this.length = length;
        }
    
        Operation(int type, String data) {
            this.type = type;
            this.data = data;
        }
    }
    

    }

  • + 0 comments

    Simple cpp solution:

    #include <iostream>
    #include <algorithm>
    #include<bits/stdc++.h>
    using namespace std;
    
    void append(string &s, string &w){
        s.append(w);
    }
    
    void deleteS(string &s, int k){
        if(s.length() == k)
            s.erase(s.begin(),s.end());
        else{
           k = s.length()-k;
           s = s.substr(0,k); 
        }    
    }
    
    void printK(string &s, int k){
        string :: iterator it;
        it = s.begin();
        advance(it,k-1);
        cout<<*it<<endl;
    }
    
    void undo(stack<string> &st, string &s){
        if(st.size() == 1){
            s = st.top();
            return;
        }
        if(!st.empty()){
            s = st.top();
            st.pop();
        }
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
        int q;
        stack<string> st;
        string s;
        cin>>q;
        while(q--){
            int x;
            string w;
            cin>>x;
            if(x==1){
                cin>>w;
                st.push(s);
                append(s,w);                        
            }
            if(x==2){
                int y;
                cin>>y;
                st.push(s);
                deleteS(s,y);            
            }
            if(x==3){
                int y;
                cin>>y;            
                printK(s,y);
            }
            if(x==4){
                undo(st,s);
            }
        } 
        return 0;
    }
    
  • + 0 comments

    JavaScript (NodeJS) Solution

    let result = "";
    const history = [""];
    
    function processCommand(input) {
        const [command, option] = input.split(" ");
        switch (command) {
            case "1":
                result += option;
                history.push(result);
                break;
            case "2":
                result = result.slice(0, result.length - option);
                history.push(result);
                break;
            case "3":
                console.log(result[option - 1]);
                break;
            case "4":
                history.pop();
                result = history.at(-1);
                break;
            default:
                break;
        }
    }
    
    function processData(input) {
        input.split(/\r?\n/).slice(1).forEach(processCommand);
    }