LeetCode-in-Java

2296. Design a Text Editor

Hard

Design a text editor with a cursor that can do the following:

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:

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:

TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)
textEditor.addText("leetcode"); // The current text is "leetcode|".
textEditor.deleteText(4); // return 4
                          // The current text is "leet|".
                          // 4 characters were deleted.
textEditor.addText("practice"); // The current text is "leetpractice|".
textEditor.cursorRight(3); // return "etpractice"
                           // The current text is "leetpractice|".
                           // The cursor cannot be moved beyond the actual text and thus did not move.
                           // "etpractice" is the last 10 characters to the left of the cursor.
 textEditor.cursorLeft(8); // return "leet"
                           // The current text is "leet|practice".
                           // "leet" is the last min(10, 4) = 4 characters to the left of the cursor.
 textEditor.deleteText(10); // return 4
                            // The current text is "|practice".
                            // Only 4 characters were deleted.
 textEditor.cursorLeft(2); // return ""
                           // The current text is "|practice".
                           // The cursor cannot be moved beyond the actual text and thus did not move.
                           // "" is the last min(10, 0) = 0 characters to the left of the cursor.
 textEditor.cursorRight(6); // return "practi"
                            // The current text is "practi|ce".
                            // "practi" is the last min(10, 6) = 6 characters to the left of the cursor. 

Constraints:

Follow-up: Could you find a solution with time complexity of O(k) per call?

Solution

public class TextEditor {
    private final StringBuilder sb;
    private int cursor;

    public TextEditor() {
        sb = new StringBuilder();
        cursor = 0;
    }

    public void addText(String text) {
        sb.insert(cursor, text);
        cursor += text.length();
    }

    public int deleteText(int k) {
        int prevPos = cursor;
        if (cursor - k >= 0) {
            cursor -= k;
            sb.delete(cursor, cursor + k);
        } else {
            sb.delete(0, cursor);
            cursor = 0;
        }
        return prevPos - cursor;
    }

    public String cursorLeft(int k) {
        cursor = Math.max(cursor - k, 0);
        return sb.substring(Math.max(cursor - 10, 0), cursor);
    }

    public String cursorRight(int k) {
        cursor = Math.min(cursor + k, sb.length());
        return sb.substring(Math.max(cursor - 10, 0), cursor);
    }
}

/*
 * Your TextEditor object will be instantiated and called as such:
 * TextEditor obj = new TextEditor();
 * obj.addText(text);
 * int param_2 = obj.deleteText(k);
 * String param_3 = obj.cursorLeft(k);
 * String param_4 = obj.cursorRight(k);
 */