Implement a text document with undo/redo based on this Python skeleton:
class Operation:
class InsertAtEndOperation(Operation):
def init(self, chars_to_insert: str):
self.chars_to_insert = chars_to_insert
class DeleteFromEndOperation(Operation):
def init(self, num_chars_to_delete: int):
self.num_chars_to_delete = num_chars_to_delete
class TextDocument:
def init(self) -> None:
Requirements:
-
apply_operation must support appending chars_to_insert to the end and deleting up to num_chars_to_delete characters from the end (no error if fewer characters exist).
-
get_current_content returns the current string; the document starts empty.
-
undo_last reverts the most recent applied operation; redo_last reapplies the most recent undone operation.
-
After any successful apply_operation, clear the redo history.
-
Ensure each operation runs in time proportional to the size of the change (avoid copying the entire document per step). Choose suitable data structures and an approach to store inverse operations.
Discuss: data structures used (e.g., stacks/chunked buffer), how inverse operations are computed, time/space complexity of each method, and key edge cases (e.g., deleting more than available, multiple undos/redos).