LeetCode 2296: Design a Text Editor Solution

Master LeetCode problem 2296 (Design a Text Editor), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To design a text editor that supports adding text, deleting text, and moving the cursor left or right, we can maintain two strings to represent the text to the left and right of the cursor. We can use a cursor position variable to keep track of the cursor's current position within the text. When adding text, we insert the new text to the right of the cursor. When deleting text, we remove characters from the left of the cursor. Moving the cursor left or right involves updating the cursor position accordingly.

Time Complexity

  • Adding text: O(k), where k is the length of the text being added.
  • Deleting text: O(min(k, len)), where k is the number of characters to delete and len is the number of characters to the left of the cursor.
  • Moving cursor left or right: O(min(k, len)), where k is the number of times to move the cursor.

Space Complexity

  • O(n), where n is the total number of characters in the text editor.

Solution Code

class TextEditor {
    StringBuilder leftText;
    StringBuilder rightText;
    int cursorPos;

    public TextEditor() {
        leftText = new StringBuilder();
        rightText = new StringBuilder();
        cursorPos = 0;
    }

    public void addText(String text) {
        rightText.insert(0, text);
    }

    public int deleteText(int k) {
        int charsToDelete = Math.min(k, leftText.length());
        leftText.delete(leftText.length() - charsToDelete, leftText.length());
        return charsToDelete;
    }

    public String cursorLeft(int k) {
        int charsToMove = Math.min(k, leftText.length());
        String movedText = leftText.substring(leftText.length() - charsToMove);
        rightText.insert(0, movedText);
        leftText.delete(leftText.length() - charsToMove, leftText.length());
        return movedText;
    }

    public String cursorRight(int k) {
        int charsToMove = Math.min(k, rightText.length());
        String movedText = rightText.substring(rightText.length() - charsToMove);
        leftText.append(movedText);
        rightText.delete(rightText.length() - charsToMove, rightText.length());
        return movedText;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2296 (Design a Text Editor)?

This page provides optimized solutions for LeetCode problem 2296 (Design a Text Editor) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2296 (Design a Text Editor)?

The time complexity for LeetCode 2296 (Design a Text Editor) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2296 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2296 in Java, C++, or Python.

Back to LeetCode Solutions