Text Editor with Undo
Asked at Figma
Reported by the problem's creator, not verified
You will build a class TextEditor that holds a piece of text, initially empty. Positions are zero-based indexes into the text.
An edit op is a list of one of these forms:
["insert", index, text]insertstextatindex["delete", start, end]removes the characters fromstartup to but not includingend["replace", start, end, text]replaces that range withtext
Each test drives one TextEditor instance through a sequence of calls.
Examples
Example 1
Input: init =
[], operations =[["apply",[["insert",0,"hello"]]],["get_text",[]],["apply",[["insert",5," world"]]],["get_text",[]],["undo",[]],["get_text",[]],["apply",[["replace",0,5,"bye"]]],["get_text",[]],["undo",[]],["get_text",[]],["undo",[]],["get_text",[]],["undo",[]],["get_text",[]]]Output:
[null,"hello",null,"hello world",null,"hello",null,"bye",null,"hello",null,"",null,""]Explanation: The first undo removes
world. Undoing thereplacerestores all five letters ofhello; the next undo removes it, and the last undo has nothing left to revert, so the text stays empty.