FastPrepCompress Editor Actions into Operational Transform Operations
Problem · Array

Compress Editor Actions into Operational Transform Operations

Learn this problem
MediumReplit logoReplitFULLTIMEPHONE SCREEN

Problem statement

You are given an original text document and a finite sequence of editor actions. A cursor starts immediately before the first character of document. Convert the actions into a compressed sequence of Operational Transform operations against the original document.

Each action is one of the following rows:

  • ["APPEND", ch] inserts the one-character string ch at the cursor and moves the cursor after it.
  • ["RIGHT"] moves the cursor over the next original-document character. If no original character remains to the right, it does nothing.
  • ["BACKSPACE"] removes the character immediately to the left of the cursor. If that character was inserted by an unmatched APPEND, the insertion is canceled. Otherwise, that original-document character becomes deleted. If there is no character to the left, the action does nothing.

Return rows representing the transformation:

  • ["INSERT", text] inserts text.
  • ["DELETE", count] deletes count original characters.
  • ["SKIP", count] keeps count original characters.

The count value is a positive decimal string. Merge every pair of adjacent operations with the same type: concatenate text for INSERT, and add counts for DELETE or SKIP. Omit canceled inserts, zero-count operations, and actions that do nothing. The unvisited suffix of document is unchanged implicitly, so do not append a final SKIP for it.

Function

compressEditorActions(document: String, actions: String[][]) → String[][]

Examples

Example 1

document = "abcd"actions = [["APPEND","x"],["APPEND","y"],["RIGHT"],["BACKSPACE"],["APPEND","z"],["RIGHT"]]return = [["INSERT","xy"],["DELETE","1"],["INSERT","z"],["SKIP","1"]]

The two initial appends merge into INSERT xy. Moving right crosses a, and the following backspace deletes that original character, so it becomes DELETE 1. Then z is inserted and b is kept.

Example 2

document = "abc"actions = [["RIGHT"],["RIGHT"],["BACKSPACE"],["BACKSPACE"],["BACKSPACE"]]return = [["DELETE","2"]]

The cursor first crosses a and b. Two backspaces delete them in reverse cursor order, and the adjacent deletions merge into DELETE 2. The final backspace has nothing left to remove.

Example 3

document = "ab"actions = [["RIGHT"],["APPEND","x"],["BACKSPACE"],["RIGHT"],["RIGHT"],["APPEND","y"]]return = [["SKIP","2"],["INSERT","y"]]

The first RIGHT keeps a. The inserted x is immediately canceled by backspace. The next successful RIGHT keeps b, the extra RIGHT does nothing at the end, and y remains inserted.

Constraints

  • 0 <= document.length <= 100000.
  • 0 <= actions.length <= 100000.
  • document contains only lowercase English letters.
  • Every action is exactly ["RIGHT"], ["BACKSPACE"], or ["APPEND", ch], where ch is one lowercase English letter.
  • The sum of document.length and the number of APPEND actions is at most 200000.
drafts saved locally
public String[][] compressEditorActions(String document, String[][] actions) {
    // Write your solution here.
}
document"abcd"
actions[["APPEND","x"],["APPEND","y"],["RIGHT"],["BACKSPACE"],["APPEND","z"],["RIGHT"]]
expected[["INSERT", "xy"], ["DELETE", "1"], ["INSERT", "z"], ["SKIP", "1"]]
checking account