• + 3 comments

    Storing the entire string after every alteration seems redunant, wouldn't it be better to store just the altered part with a flag variable denoting the type of operation last performed?

    Here is my accepted code in python3.

    n = int(input())
    stack = []
    stri = ''
    for i in range(n):
        l = list(input().split())
        if(int(l[0]) == 1):
            stri += str(l[1])
            stack.append(len(str(l[1])))
        if(int(l[0]) == 2):
            stack.append(stri[-int(l[1]):])
            stri = stri[:-int(l[1])]
        if(int(l[0]) == 3):
            print(stri[int(l[1]) - 1])                    
        if(int(l[0]) == 4):
            x = stack.pop()
            if(type(x) is int):
                stri = stri[:-x]
            else:
                stri+=x