Undo

Write code · Medium · §5.1 Stacks

commands holds, in order, the commands given to a text box. "type" followed by a space and a word (for example, "type hi") adds the word to the end of the text, with no space before it. "undo" cancels one type command: the most recent one that has not been undone yet.

If there is no type command to undo, undo does nothing. Complete the function solution, which returns the text after all the commands are run.

Function

solution(commands)

Constraints

Examples

commandsresult
['type ab', 'type cd', 'undo', 'type e']'abe'
['undo', 'type x']'x'

Starter code

def solution(commands):
    answer = ""
    return answer

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.