All problems

Text Editor Cursor

Medium · design

A small text editor starts with empty text and a blinking cursor. The cursor sits between two characters (or at either end of the text), and it begins at position 0. You are given a list of commands, run in order. Each command is a name, one space, and an argument:

Return a list with everything recorded by the "look" commands, in the order the commands were given.

Examples

Example 1

Input: commands = ["insert hello", "left 2", "insert xy", "look 6"]

Output: ["helxy"]

After hello, two steps left put the cursor between hel and lo. The text becomes helxylo with the cursor after xy, so the five characters to its left are all there is.

Example 2

Input: commands = ["insert abc", "delete 2", "look 5", "left 5", "delete 1", "right 9", "insert z", "look 3"]

Output: ["a", "az"]

Deleting two leaves a. The cursor goes to the start, where there is nothing to delete, then to the end after the a. Inserting z there gives az.

Example 3

Input: commands = ["look 3", "insert go", "left 1", "look 3", "right 1", "look 1"]

Output: ["", "g", "o"]

The first look finds an empty text. With the cursor between g and o, only g is on its left. After moving right, the last character on its left is o.

Constraints