1610. Undoable Line Editor

Hard7.5 points estimatedDesignDoubly-Linked ListStackString

Design UndoableEditor, a one-line text editor that starts empty with the cursor at index 0 (the cursor index is the number of characters to its left). typeText(text) inserts text at the cursor, leaves the cursor just after it and returns nothing. erase(k) deletes up to k characters immediately left of the cursor and returns how many were deleted. moveLeft(k) and moveRight(k) move the cursor by up to k places and return its new index. undo() reverts the most recent typeText or erase call that has not been undone yet, even one that changed nothing, puts the removed text back or takes the typed text out, and returns the cursor to where it was just before that call; it returns false when there is nothing to undo. Moves cannot be undone. currentText() returns the whole line. Texts and k are at most 40 and there are at most 1000 calls; store the characters in a doubly linked list.

Example 1

Input:
operations = ["UndoableEditor","typeText","erase","currentText","undo","currentText","moveLeft","typeText"]arguments = [[],["hello"],[2],[],[],[],[3],["XY"]]
Output:
[null,null,2,"hel",true,"hello",2,null]
Explanation:

Erasing 2 leaves hel, undo restores hello with the cursor back at 5, then typing XY after moving left 3 gives heXYllo.

Example 2

Input:
operations = ["UndoableEditor","undo","currentText","erase","moveRight"]arguments = [[],[],[],[4],[3]]
Output:
[null,false,"",0,0]
Explanation:

On an empty editor nothing can be undone, erased or moved.

Constraints

1 ≤ text.length ≤ 40, lowercase letters

1 ≤ k ≤ 40

at most 1000 calls

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Input
Each case is an operation log. operations names the class first and then each method call; arguments holds the arguments for each, in the same order. Your answer is one list with a result per operation - null for the constructor and for methods that return nothing.

Expected complexity

Time
O(k) per edit, O(L) for currentText
Space
O(L)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…

operations names the class, then each method to call; arguments holds one list of arguments per operation, in the same order.