#2296

Design a Text Editor

Hard
Linked ListStringStackDesignSimulationDoubly-Linked ListDoubly Linked ListSimulation
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

The optimal approach uses a doubly linked list to represent the text, allowing efficient insertions and deletions at both ends. This structure allows us to manage the text and cursor position without needing to manipulate a large string frequently.

⚙️

Algorithm

5 steps
  1. 1Step 1: Use a doubly linked list where each node represents a character.
  2. 2Step 2: Maintain two pointers: one for the current node (cursor) and one for the head of the list.
  3. 3Step 3: For 'addText', create new nodes and link them at the cursor position.
  4. 4Step 4: For 'deleteText', traverse left from the cursor and remove nodes as needed.
  5. 5Step 5: For 'cursorLeft' and 'cursorRight', move the cursor pointer accordingly.
solution.py49 lines
1class Node:
2    def __init__(self, char):
3        self.char = char
4        self.prev = None
5        self.next = None
6
7class TextEditor:
8    def __init__(self):
9        self.head = None
10        self.cursor = None
11
12    def addText(self, text: str) -> None:
13        for char in text:
14            new_node = Node(char)
15            if not self.head:
16                self.head = new_node
17                self.cursor = new_node
18            else:
19                new_node.prev = self.cursor
20                self.cursor.next = new_node
21                self.cursor = new_node
22
23    def deleteText(self, k: int) -> int:
24        delete_count = 0
25        while self.cursor and delete_count < k:
26            self.cursor = self.cursor.prev
27            delete_count += 1
28        return delete_count
29
30    def cursorLeft(self, k: int) -> str:
31        for _ in range(k):
32            if self.cursor:
33                self.cursor = self.cursor.prev
34        return self.getLeftChars()
35
36    def cursorRight(self, k: int) -> str:
37        for _ in range(k):
38            if self.cursor and self.cursor.next:
39                self.cursor = self.cursor.next
40        return self.getLeftChars()
41
42    def getLeftChars(self):
43        chars = []
44        current = self.cursor
45        for _ in range(10):
46            if current:
47                chars.append(current.char)
48                current = current.prev
49        return ''.join(reversed(chars))

Complexity note: The time complexity is O(n) for adding or deleting text because we traverse the linked list to create or remove nodes, but each operation is efficient due to the linked structure.

  • 1Using a doubly linked list allows for efficient insertions and deletions, especially when managing a cursor.
  • 2Maintaining a cursor pointer helps in tracking the current position without needing to slice strings.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.