920. Cursor Text Editor

A vintage typewriter has been given a memory. Build the class CursorEditor (no constructor arguments). The text starts empty and a cursor sits between characters.

  • addText(text) inserts text at the cursor; the cursor ends up right after the inserted text.
  • deleteText(k) erases up to k characters immediately to the left of the cursor and returns how many were actually erased.
  • cursorLeft(k) and cursorRight(k) move the cursor by up to k characters, stopping at the ends of the text. After the move, return the last min(10, len) characters to the left of the cursor, where len is the number of characters left of it (empty string if none).

Each operation should take time proportional to the characters it adds, deletes or moves, never to the whole text.

Example 1

Input:
operations = ["CursorEditor","addText","deleteText","addText","cursorLeft","cursorLeft","cursorRight","deleteText"]arguments = [[],["quartz"],[2],["ck"],[3],[9],[2],[1]]
Output:
[null,null,2,null,"qua","","qu",1]
Explanation:

The text becomes quarck. Moving left 3 puts the cursor after 'qua' (the view); moving left 9 stops at the start (empty view); moving right 2 shows 'qu'; one deletion removes the 'u'.

Example 2

Input:
operations = ["CursorEditor","addText","cursorLeft","addText","cursorRight","deleteText","deleteText"]arguments = [[],["mango pie"],[4],["!!"],[10],[100],[1]]
Output:
[null,null,"mango",null,"ango!! pie",11,0]
Explanation:

The cursor sits after 'mango', '!!' is inserted there, moving right clamps at the end, deleting 100 removes the whole 11-character text and the next delete removes nothing.

Constraints

1 ≤ text.length ≤ 40, lowercase letters and spaces

1 ≤ k ≤ 100

At most 3000 calls in total

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 call
Space
O(n)

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.