Dropbox
Design a Text Editor
Codinghard
Problem
Design a text editor with a cursor that can insert text, delete text immediately to the left of the cursor, and move the cursor in either direction.
Implement the TextEditor class:
TextEditor()Initializes the editor with empty text.void addText(string text)Insertstextimmediately to the left of the cursor. The cursor remains after the inserted text.int deleteText(int k)Deletes up tokcharacters immediately to the left of the cursor and returns the number deleted.string cursorLeft(int k)Moves the cursor up tokpositions left and returns the last at most10characters to the left of the cursor.string cursorRight(int k)Moves the cursor up tokpositions right and returns the last at most10characters to the left of the cursor.
Examples
Example 1:
Input: ["TextEditor", "addText", "deleteText", "addText", "cursorRight", "cursorLeft", "deleteText", "cursorLeft", "cursorRight"] [[], ["leetcode"], [4], ["practice"], [3], [8], [10], [2], [6]]
Output: [null, null, 4, null, "etpractice", "leet", 4, "", "practi"]
Explanation:
After inserting leetcode, four characters are deleted. Inserting practice produces leetpractice|. Moving and deleting around the cursor yields the returned previews shown above.
Constraints
1 <= text.length, k <= 40textconsists of lowercase English letters.- The total length of all text passed to
addTextis at most4 * 10^5. - The sum of all
kvalues passed to cursor and delete operations is at most4 * 10^5.
Solution
Loading editor…
Sign in to get AI feedback on your answer. Your work is saved while you do.
Sign in to evaluate