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)insertstextat the cursor; the cursor ends up right after the inserted text.deleteText(k)erases up tokcharacters immediately to the left of the cursor and returns how many were actually erased.cursorLeft(k)andcursorRight(k)move the cursor by up tokcharacters, stopping at the ends of the text. After the move, return the lastmin(10, len)characters to the left of the cursor, wherelenis 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.
operationsnames the class first and then each method call;argumentsholds the arguments for each, in the same order. Your answer is one list with a result per operation -nullfor the constructor and for methods that return nothing.
Expected complexity
- Time
- O(k) per call
- Space
- O(n)