Design a text editor with a cursor that can do the following:


	Add text to where the cursor is.
	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 also remain within the actual text and cannot be moved beyond it. More formally, we have that 0 &lt;= cursor.position &lt;= currentText.length always holds.

Implement the TextEditor class:


	TextEditor() Initializes the object with empty text.
	void addText(string text) Appends text to where the cursor is. The cursor ends to the right of text.
	int deleteText(int k) Deletes k characters to the left of the cursor. Returns the number of characters actually deleted.
	string cursorLeft(int k) Moves the cursor to the left k times. Returns the last min(10, len) characters to the left of the cursor, where len is the number of characters to the left of the cursor.
	string cursorRight(int k) Moves the cursor to the right k times. Returns the last min(10, len) characters to the left of the cursor, where len is the number of characters to the left of the cursor.


&nbsp;
Example 1:

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"]

Explanation
TextEditor textEditor = new TextEditor(); // The current text is "|". (The '|' character represents the cursor)
textEditor.addText("leetcode"); // The current text is "leetcode|".
textEditor.deleteText(4); // return 4
                          // The current text is "leet|". 
                          // 4 characters were deleted.
textEditor.addText("practice"); // The current text is "leetpractice|". 
textEditor.cursorRight(3); // return "etpractice"
                           // The current text is "leetpractice|". 
                           // The cursor cannot be moved beyond the actual text and thus did not move.
                           // "etpractice" is the last 10 characters to the left of the cursor.
textEditor.cursorLeft(8); // return "leet"
                          // The current text is "leet|practice".
                          // "leet" is the last min(10, 4) = 4 characters to the left of the cursor.
textEditor.deleteText(10); // return 4
                           // The current text is "|practice".
                           // Only 4 characters were deleted.
textEditor.cursorLeft(2); // return ""
                          // The current text is "|practice".
                          // The cursor cannot be moved beyond the actual text and thus did not move. 
                          // "" is the last min(10, 0) = 0 characters to the left of the cursor.
textEditor.cursorRight(6); // return "practi"
                           // The current text is "practi|ce".
                           // "practi" is the last min(10, 6) = 6 characters to the left of the cursor.


&nbsp;
Constraints:


	1 &lt;= text.length, k &lt;= 40
	text consists of lowercase English letters.
	At most 2 * 104 calls in total will be made to addText, deleteText, cursorLeft and cursorRight.


&nbsp;
Follow-up: Could you find a solution with time complexity of O(k) per call?

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]
class TextEditor {
    StringBuilder res;
    int pos=0;

    public TextEditor() {
        res = new StringBuilder();
    }

    public void addText(String text) {
        res.insert(pos,text);
        pos += text.length();
    }

    public int deleteText(int k) {
        int tmp = pos;
        pos -= k;
        if(pos<0) pos=0;
        res.delete(pos,tmp);
        return tmp-pos;
    }

    public String cursorLeft(int k) {
        pos-=k;
        if(pos<0) pos = 0;
        if(pos<10) return res.substring(0,pos);
        return res.substring(pos-10,pos);
    }

    public String cursorRight(int k) {
        pos+=k;
        if(pos>res.length()) pos = res.length();
        if(pos<10) return res.substring(0,pos);
        return res.substring(pos-10,pos);
    }
}
class TextEditor {
    stack<char> left;
    stack<char> right;
public:
    TextEditor() {

    }

    void addText(string text) {
        for(auto &c : text){
            left.push(c);
        }
    }

    int deleteText(int k) {
        int cnt=0;
        while(!left.empty() and k>0){
            left.pop();
            cnt++;
            k--;
        }
        return cnt;
    }

    string cursorLeft(int k) {
        while(!left.empty() and k>0){
            char c = left.top();left.pop();
            right.push(c);
            k--;
        }
        // returning the last min(10, len) characters to the left of the cursor
        return cursorShiftString();
    }

    string cursorRight(int k) {
        while(!right.empty() and k>0){
            char c = right.top();right.pop();
            left.push(c);
            k--;
        }
        // returning the last min(10, len) characters to the left of the cursor
        return cursorShiftString();
    }

    // function to return the last min(10, len) characters to the left of the cursor
    string cursorShiftString(){
        string rtn = "";
        int cnt=10;
        while(!left.empty() and cnt>0){
            char c = left.top();left.pop();
            rtn += c;
            cnt--;
        }
        reverse(rtn.begin(),rtn.end());
        for(int i=0;i<rtn.size();i++){
            left.push(rtn[i]);
        }
        return rtn;
    }
};
var TextEditor = function() {
    this.forward = [];
    this.backward = [];
};

/** 
 * @param {string} text
 * @return {void}
 */
TextEditor.prototype.addText = function(text) {
    for (let letter of text) {
        this.forward.push(letter);
    }
};

/** 
 * @param {number} k
 * @return {number}
 */
TextEditor.prototype.deleteText = function(k) {
    let deleted = 0;
    while (this.forward.length && deleted < k) {
        this.forward.pop();
        deleted++;
    }
    return deleted;
};

/** 
 * @param {number} k
 * @return {string}
 */
TextEditor.prototype.cursorLeft = function(k) {
    let moved = 0;
    while (this.forward.length && moved < k) {
        this.backward.push(this.forward.pop());
        moved++;
    }
    return toTheLeft(this.forward);
};

/** 
 * @param {number} k
 * @return {string}
 */
TextEditor.prototype.cursorRight = function(k) {
    let moved = 0;
    while (moved < k && this.backward.length) {
        this.forward.push(this.backward.pop());
        moved++;
    }
    return toTheLeft(this.forward);
};


function toTheLeft (arr) {
    let letters = [];
    for (let i = Math.max(0, arr.length - 10); i < arr.length; i++) {
        letters.push(arr[i]);
    }
    let res = letters.join("");
    return res; 
}
Design a Text Editor