Design a text editor with a cursor that can perform the following operations:

  • Add text to where the cursor is positioned
  • Delete text from where the cursor is (simulating the backspace key)
  • Move the cursor either left or right

When deleting text, only characters to the left of the cursor will be deleted. The cursor will always remain within the actual text boundaries: 0 ≤ cursor.position ≤ currentText.length.

Implement the TextEditor class with these methods:

  • TextEditor() - Initializes the object with empty text
  • void addText(string text) - Appends text at cursor position, cursor moves to right of added text
  • int deleteText(int k) - Deletes k characters to the left of cursor, returns actual number of characters deleted
  • string cursorLeft(int k) - Moves cursor left k times, returns last min(10, len) characters to the left of cursor
  • string cursorRight(int k) - Moves cursor right k times, returns last min(10, len) characters to the left of cursor

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["TextEditor"],["addText","leetcode"],["deleteText",4],["addText","practice"],["cursorRight",3],["cursorLeft",8]]
Output: [null,null,4,null,"etpractice","leet"]
💡 Note: TextEditor textEditor = new TextEditor(); // Initially text="", cursor=0 textEditor.addText("leetcode"); // text="leetcode", cursor=8 textEditor.deleteText(4); // return 4, text="leet", cursor=4 textEditor.addText("practice"); // text="leetpractice", cursor=12 textEditor.cursorRight(3); // return "etpractice" (cursor at end, last 10 chars) textEditor.cursorLeft(8); // return "leet" (cursor at pos 4, 4 chars to left)
Example 2 — Cursor Movements
$ Input: operations = [["TextEditor"],["addText","hello"],["cursorLeft",3],["addText","world"],["cursorRight",2]]
Output: [null,null,"he",null,"heworldl"]
💡 Note: Start empty, add "hello" (cursor at end), move left 3 positions (cursor after 'e'), add "world", move right 2 positions. Final text: "heworldllo"
Example 3 — Edge Case Boundaries
$ Input: operations = [["TextEditor"],["cursorLeft",5],["addText","a"],["deleteText",10],["cursorRight",5]]
Output: [null,"",null,1,""]
💡 Note: Try to move left from empty (returns ""), add 'a', try to delete 10 chars (only deletes 1), try to move right from empty (returns "")

Constraints

  • 1 ≤ text.length, k ≤ 40
  • text consists of lowercase English letters
  • At most 2 × 104 calls in total will be made to addText, deleteText, cursorLeft and cursorRight

Visualization

Tap to expand
Text Editor Design: Cursor Management SystemOperationsaddText, deleteTextcursorLeft, cursorRightText Buffer"hello|world"cursor positionReturn ValuesDeleted count orlast 10 charsTwo Stacks ImplementationLeft Stack:['h','e','l','l','o']Right Stack:['d','l','r','o','w']All Operations: O(1) Time ComplexityEfficient cursor movements using stack operations
Understanding the Visualization
1
Input Operations
Sequence of text editor commands: add, delete, cursor moves
2
Process Commands
Execute each operation while maintaining cursor position
3
Output Results
Return results for methods that have return values
Key Takeaway
🎯 Key Insight: Two stacks perfectly simulate a text cursor - left stack holds text before cursor, right stack holds text after cursor, enabling O(1) operations
Asked in
Google 25 Microsoft 20 Amazon 18 Meta 15
31.5K Views
Medium Frequency
~35 min Avg. Time
892 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