Header Ad

HackerRank Simple Text Editor problem solution

In this HackerRank Simple Text Editor problem solution, we need to implement a simple text editor that can append, delete or print a character and also undo the last move.

HackerRank Simple Text Editor problem solution


Problem solution in Python programming.

no_ops = int(input())
ops = []
s = []
for i in range(no_ops):
    one_op = input().split(' ')
    ops.append(one_op)
    
from copy import copy
history = []
for op in ops:
    #print(s)
    #print(op)
    if op[0] == '1':
        to_append = op[1]
        history.append(copy(s))
        s.extend(to_append)
    elif op[0] == '2':
        k = int(op[1])
        history.append(copy(s))
        del s[-k:]
    elif op[0] == '3':
        k = int(op[1])
        print(s[k-1])
    elif op[0] == '4':
        s = history.pop()


Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long Q = sc.nextInt();

        int tag, k;
        String last, newString;
        Stack<String> stack = new Stack<>();

        while(Q-->0){
            tag = sc.nextInt();
            switch (tag){
                case 1:
                    //append W
                    last = stack.size() > 0 ? stack.peek() : "";
                    newString = last + sc.next();
                    //System.out.println(tag + " " + newString);
                    stack.push(newString);
                    break;
                case 2:
                    //erase last k character of S
                    k = sc.nextInt();
                    last = stack.peek();
                    newString = last.substring(0, last.length()-k);
                    //System.out.println(tag + " " + newString);
                    stack.push(newString);
                    break;
                case 3:
                    //return kth character of S
                    k = sc.nextInt()-1;
                    if(stack.size() > 0) {
                        last = stack.peek();
                        String c = String.valueOf(last.charAt(k));
                        //System.out.println(tag + " " + c);
                        System.out.println(c);
                    }
                    break;
                case 4:
                    //undo
                    //System.out.println(tag + " " + stack.peek());
                    stack.pop();
                    break;
            }
        }
    }
}


Problem solution in C++ programming.

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <tuple>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

typedef struct query
{
    int type, remove;
    string add;
}query;

char stak[1000003];

int topp = 0;

void push(string s)
{
    for(int i = 0 ; i < s.length(); i++)
    {
        stak[++topp] = s[i];
    }
    assert(topp <= 1000000);
}

string pop(int remove)
{
    assert(remove >= 0 && remove <= topp);
    string popped = "";
    int del = remove;
    for (int  i = topp; del > 0; i--, del--) {
        popped = stak[i] + popped;
    }
    topp -= remove;
    return popped;
}

int main()
{
    int t, type, remove, k;
    stack<query> q_stack;
    cin>>t;
    while(t--)
    {
        cin>>type;
        if(type == 1)
        {
            string add;
            cin>>add;
            push(add);
            query last;
            last.type = type;
            last.add = add;
            q_stack.push(last);
        }
        else if(type == 2)
        {
            cin>>remove;
            string popped = pop(remove);
            query last;
            last.type = type;
            last.remove = remove;
            last.add = popped;
            q_stack.push(last);
        }
        else if(type == 3)
        {
            cin>>k;
            assert(k >= 1 && k <= topp);
            cout<<stak[k]<<endl;
        }
        else
        {
            query last = q_stack.top();
            q_stack.pop();
            if(last.type == 1)
            {
                int remove = (last.add).length();
                string popped = pop(remove);
            }
            else
            {
                push(last.add);
                assert(topp >= 1 && topp <= 1000000);
            }
        }
    }
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>


int main() {

    FILE * in = stdin;
    FILE * out = stdout;
   
    int command;
    int k;
    char buf[1000000];
    char ** undos;
    int n, i, ptr = 0;
    size_t len;
    fscanf(in, "%d", &n);
    undos = calloc(sizeof(char *), n);
    undos[ptr] = malloc(sizeof(char));
    strcpy(undos[ptr], "");
    for (i = 0; i < n; ++i) {
        fscanf(in, "%d", &command);
        switch(command) {
            case 1:
                fscanf(in, "%1000000s\n", buf);
                ++ptr;
                undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr-1]) + strlen(buf) + 1));
                strcpy(undos[ptr], undos[ptr-1]);
                strcat(undos[ptr], buf);
                break;
            case 2:
                fscanf(in, "%d", &k);
                ++ptr;
                undos[ptr] = malloc(sizeof(char) * (strlen(undos[ptr - 1]) - k + 1));
                len = strlen(undos[ptr - 1]);
                memcpy(undos[ptr], undos[ptr-1], strlen(undos[ptr - 1]) - k);
                undos[ptr][len - k] = 0;
                break;
            case 3:
                fscanf(in, "%d", &k);
                fprintf(out, "%c\n", undos[ptr][k-1]);
                break;
            case 4:
                --ptr;
                break;
        }
    }
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var lines = input.split("\n");
    var str = "";
    var last = [];
    
    for (var i=1; i <= parseInt(lines[0]); i++) {
        var command = parseInt(lines[i].split(" ")[0]),
            args = lines[i].split(" ")[1];

        switch (command) {
            case 1:
                last.push(str);
                str = str + args;
                break;
            case 2:
                last.push(str);
                str = str.substring(0, str.length - parseInt(args));
                break;
            case 3:
                console.log(str.charAt(parseInt(args) - 1));
                break;
            case 4:
                str = last.pop();
                break;
        }
        
    }
    
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments