Undo Text Editor

Stack Undo Text Editor

Problem: Undo Text Editor

You need to simulate a simple text editor that supports three operations:

  1. append : Append the given string to the end of the current text.
  2. delete : Delete the last k characters from the current text. If k is larger than the current text length, delete all characters.
  3. undo: Undo the last operation that modified the text (i.e., an append or delete). If there is no operation to undo, the text remains unchanged.

Your task is to implement a function that processes a sequence of these operations and returns the final state of the text.

Example:

Input operations: ['append hello', 'append world', 'delete 5', 'undo']

  • After append hello: text is "hello"
  • After append world: text is "helloworld"
  • After delete 5: text is "hello"
  • After undo: the delete is undone so the text goes back to "helloworld"

Return the final text after processing all operations.

Hint: Consider using a stack to store the previous states or operations to efficiently implement the undo functionality.

Good luck!