2296. Design a Text Editor
Description
Design a text editor with a cursor that can do the following:
- Add text to where the cursor is.
- Delete text from where the cursor is (simulating the backspace key).
- Move the cursor either left or right.
When deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that 0 <= cursor.position <= currentText.length always holds.
Implement the TextEditor class:
TextEditor()Initializes the object with empty text.void addText(string text)Appendstextto where the cursor is. The cursor ends to the right oftext.int deleteText(int k)Deleteskcharacters to the left of the cursor. Returns the number of characters actually deleted.string cursorLeft(int k)Moves the cursor to the leftktimes. Returns the lastmin(10, len)characters to the left of the cursor, wherelenis the number of characters to the left of the cursor.string cursorRight(int k)Moves the cursor to the rightktimes. Returns the lastmin(10, len)characters to the left of the cursor, wherelenis the number of characters to the left of the cursor.
Example 1:
1 | Input |
Constraints:
1 <= text.length, k <= 40textconsists of lowercase English letters.- At most
2 * 10^4calls in total will be made toaddText,deleteText,cursorLeftandcursorRight.
Follow-up: Could you find a solution with time complexity of O(k) per call?
Hints/Notes
- 2025/02/20 Q3
- Doubly-Linked List
- Leetcode solution
Solution
Language: C++
1 | class TextEditor { |