forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesign a Text Editor.py
27 lines (22 loc) · 910 Bytes
/
Design a Text Editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Runtime: 1865 ms (Top 50.36%) | Memory: 27.8 MB (Top 82.98%)
class TextEditor:
def __init__(self):
self.s = ''
self.cursor = 0
def addText(self, text: str) -> None:
self.s = self.s[:self.cursor] + text + self.s[self.cursor:]
self.cursor += len(text)
def deleteText(self, k: int) -> int:
new_cursor = max(0, self.cursor - k)
noOfChars = k if self.cursor - k >= 0 else self.cursor
self.s = self.s[:new_cursor] + self.s[self.cursor:]
self.cursor = new_cursor
return noOfChars
def cursorLeft(self, k: int) -> str:
self.cursor = max(0, self.cursor - k)
start = max(0, self.cursor-10)
return self.s[start:self.cursor]
def cursorRight(self, k: int) -> str:
self.cursor = min(len(self.s), self.cursor + k)
start = max(0, self.cursor - 10)
return self.s[start:self.cursor]