Design a Text Editor
candidate master · 1455 · lc hard +32 · 50.1% accepted · 654 likes · top 38%
Description
Design a text editor with a cursor that can:
- Insert text at the current cursor position.
- Delete characters immediately to the left of the cursor.
- Move the cursor left or right.
The cursor stays within the text at all times: 0 <= cursor.position <= currentText.length.
Implement the TextEditor class:
- TextEditor() Creates an editor with empty text.
- void addText(string text) Inserts text at the cursor; cursor ends up to the right of the inserted text.
- int deleteText(int k) Deletes up to k characters to the left; returns the actual number deleted.
- string cursorLeft(int k) Moves cursor left up to k steps; returns up to 10 characters immediately left of the cursor.
- string cursorRight(int k) Moves cursor right up to k steps; returns up to 10 characters immediately left of the cursor.
Example 1:
Example 2:
Code