Design a Text Editor with cursor navigation and text manipulation capabilities.

You need to implement a text editor that supports a movable cursor with the following operations:

  • Add text at the cursor position
  • Delete text to the left of the cursor (backspace functionality)
  • Move cursor left or right within the text

The cursor behaves like a real text editor - it stays within bounds 0 ≤ cursor.position ≤ text.length and deletion only affects characters to the left.

Example:
Starting with empty text "" and cursor at position 0:
addText("hello") → text becomes "hello|" (cursor at end)
cursorLeft(2) → cursor moves to "hel|lo"
addText("p") → text becomes "help|lo"
deleteText(1) → text becomes "hel|lo"

Input & Output

example_1.py — Basic Operations
$ 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"]
💡 Note: TextEditor textEditor = new TextEditor(); // current text is "". cursor is at index 0. textEditor.addText("leetcode"); // current text is "leetcode". cursor is at index 8. textEditor.deleteText(4); // return 4. current text is "leet". cursor is at index 4. textEditor.addText("practice"); // current text is "leetpractice". cursor is at index 12. textEditor.cursorRight(3); // return "etpractice". current text is "leetpractice". cursor is at index 12. textEditor.cursorLeft(8); // return "leet". current text is "leetpractice". cursor is at index 4. textEditor.deleteText(10); // return 4. current text is "practice". cursor is at index 0. textEditor.cursorLeft(2); // return "". current text is "practice". cursor is at index 0. textEditor.cursorRight(6); // return "practi". current text is "practice". cursor is at index 6.
example_2.py — Empty Text Operations
$ Input: ["TextEditor", "cursorLeft", "cursorRight", "deleteText"] [[], [5], [3], [2]]
Output: [null, "", "", 0]
💡 Note: All operations on empty text return appropriate default values. Cursor stays at position 0, and deleteText returns 0 since no characters to delete.
example_3.py — Boundary Conditions
$ Input: ["TextEditor", "addText", "cursorLeft", "cursorLeft", "cursorRight", "cursorRight"] [[], ["ab"], [1], [5], [1], [5]]
Output: [null, null, "a", "", "a", "ab"]
💡 Note: Tests cursor movement beyond text boundaries. cursorLeft(5) moves only 1 position (to beginning), cursorRight(5) moves only 1 position (to end).

Visualization

Tap to expand
Text Editor ImplementationLeft Stack (Before Cursor)Right Stack (After Cursor)hello!CURSORText: "hell|o!"All Operations: O(1) Amortized Time
Understanding the Visualization
1
Initialize
Start with two empty stacks representing cursor at beginning
2
Add Text
Push new characters onto the left stack (before cursor)
3
Move Cursor
Transfer characters between stacks to simulate cursor movement
4
Delete Text
Pop characters from left stack (backspace behavior)
Key Takeaway
🎯 Key Insight: Two stacks naturally represent cursor position - left stack = text before cursor, right stack = text after cursor. This makes all text editor operations incredibly efficient!

Time & Space Complexity

Time Complexity
⏱️
O(1)

All operations are O(1) amortized. Stack push/pop operations are O(1), cursor movements transfer characters between stacks in O(k) where k is movement distance

n
2n
Linear Growth
Space Complexity
O(n)

Two stacks store all n characters of the text

n
2n
Linearithmic Space

Constraints

  • 1 ≤ text.length ≤ 50
  • 1 ≤ k ≤ 50
  • text consists of lowercase English letters.
  • At most 2 × 104 calls in total will be made to addText, deleteText, cursorLeft and cursorRight.
  • The sum of all text.length over all addText calls is at most 2 × 104
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 22
42.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen