Build a text editor class with the following methods:
class TextEditor {
void moveCursorLeft();
void moveCursorRight();
void insertCharacter(char); // insert the char right before cursor
void backspace(); // delete the char right before cursor
}Follow-up:
Implement undo(); //undo the last edit. Can be called multiple times until all edits are cancelled.
All methods above should take O(1) time.
Example: ( | denotes where the cursor locates. text shows what's been written to the text editor)
Start with empty text
text = "|"
insertCharacter('a')
text = "a|"
insertCharacter('b')
text = "ab|"
insertCharacter('c')
text = "abc|"
moveCursorLeft()
text = "ab|c"
moveCursorLeft()
text = "a|bc"
backspace()
text = "|bc"
moveCursorLeft()
text = "|bc" (nothing happens since cursor was on the leftmost position)
undo()
text = "a|bc"
undo()
text = "ab|c"
undo()
text = "abc|"
undo()
text = "ab|"
undo()
text = "a|"