Problem statement

https://binarysearch.com/problems/Text-Editor/

Solution

Use stack where we simulate process. Be careful about <-. Alternative way is to use split.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        stack, n = [], len(s)
        i = 0
        while i < n:
            if i + 1 < n and s[i] == "<" and s[i + 1] == "-":
                if stack: stack.pop()
                i += 2
            else:
                stack += [s[i]]
                i += 1

        return "".join(stack)