Similar Problems

Similar Problems not available

Design A Text Editor - Leetcode Solution

Companies:

LeetCode:  Design A Text Editor Leetcode Solution

Difficulty: Hard

Topics: string stack design linked-list simulation  

Design A Text Editor is a problem on LeetCode that requires you to design a basic text editor. The text editor should have the following functionalities:

  1. Insert a character at the current cursor position.
  2. Delete a character at the current cursor position.
  3. Move the cursor left or right by one position.
  4. Get the current content of the text editor.

To solve this problem, we can use a linked list data structure to represent the characters in the text editor. Each node in the linked list will represent a character and will contain a pointer to the next node in the list.

We can also use a cursor pointer to keep track of the current position in the editor. The cursor pointer will initially point to the head of the linked list.

Here is a detailed solution to the Design A Text Editor problem on LeetCode:

  1. Define a Node class
class Node:
    def __init__(self, char):
        self.char = char
        self.next = None
  1. Define a TextEditor class
class TextEditor:
    def __init__(self):
        self.cursor = Node('')
        self.head = self.cursor

The TextEditor class initializes the cursor pointer to an empty node and sets the head of the linked list to the cursor node.

  1. Define insert() method to insert a character at the current cursor position
    def insert(self, char):
        node = Node(char)
        node.next = self.cursor.next
        self.cursor.next = node
        self.cursor = node

The insert() method creates a new node with the given character and inserts it after the current cursor position. It then updates the cursor pointer to point to the newly inserted node.

  1. Define delete() method to delete the character at the current cursor position
    def delete(self):
        if self.cursor.next is None:
            return
        node = self.cursor.next
        self.cursor.next = node.next
        del node

The delete() method deletes the node after the current cursor position. It first checks if the cursor is at the end of the linked list. If it is, the method does nothing. Otherwise, it removes the node after the cursor and updates the next pointer of the cursor to point to the next node in the list.

  1. Define left() method to move the cursor one position to the left
    def left(self):
        if self.cursor == self.head:
            return
        node = self.head
        while node.next != self.cursor:
            node = node.next
        self.cursor = node

The left() method moves the cursor one position to the left. It first checks if the cursor is already at the beginning of the list. If it is, the method does nothing. Otherwise, it traverses the list from the head to find the node before the cursor and updates the cursor pointer to point to that node.

  1. Define right() method to move the cursor one position to the right
    def right(self):
        if self.cursor.next is None:
            return
        self.cursor = self.cursor.next

The right() method moves the cursor one position to the right. It first checks if the cursor is already at the end of the list. If it is, the method does nothing. Otherwise, it updates the cursor pointer to point to the next node in the list.

  1. Define get_content() method to return the current content of the text editor
    def get_content(self):
        node = self.head.next
        content = ''
        while node:
            content += node.char
            node = node.next
        return content

The get_content() method returns the current content of the text editor as a string. It first starts at the head of the list and traverses the list to the end, concatenating the characters of each node to the content string.

With these methods defined, we now have a basic text editor that can be used to insert, delete, and navigate through characters, and also retrieve the current content of the editor.

Example Usage:

editor = TextEditor()
editor.insert('h')
editor.insert('e')
editor.insert('l')
editor.insert('l')
editor.insert('o')
print(editor.get_content())  # 'hello'
editor.left()
editor.left()
editor.delete()
print(editor.get_content())  # 'helo'
editor.right()
editor.insert('l')
print(editor.get_content())  # 'hello'

Output:

'hello'
'helo'
'hello'

Design A Text Editor Solution Code

1