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
- The length of
commandsis from 1 to 1,000. - Each command is
"undo", or"type"followed by a space and a word. Each word is made of lowercase English letters, with a length from 1 to 10.
Examples
| commands | result |
|---|---|
['type ab', 'type cd', 'undo', 'type e'] | 'abe' |
['undo', 'type x'] | 'x' |
Starter code
def solution(commands):
answer = ""
return answerSolve 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.