JJobsMoi
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) Inserts text immediately to the left of the cursor. The cursor remains after the inserted text.
  • int deleteText(int k) Deletes up to k characters immediately to the left of the cursor and returns the number deleted.
  • string cursorLeft(int k) Moves the cursor up to k positions left and returns the last at most 10 characters to the left of the cursor.
  • string cursorRight(int k) Moves the cursor up to k positions right and returns the last at most 10 characters 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 <= 40
  • text consists of lowercase English letters.
  • The total length of all text passed to addText is at most 4 * 10^5.
  • The sum of all k values passed to cursor and delete operations is at most 4 * 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