Document Edit Events
Problem statement
A document starts as the empty string. Process each event in order and return the final document.
["INSERT", position, text]insertstextbefore the zero-basedposition.["DELETE", position, length]removeslengthcharacters beginning atposition.
An event whose position or range is invalid for the current document is rejected and leaves the document unchanged.
Function
applyDocumentEdits(events: String[][]) → StringExamples
Example 1
events = [["INSERT","0","hello"],["INSERT","5"," world"],["DELETE","5","1"]]return = "helloworld"The second event adds a leading space before world, and the delete removes that space.
Example 2
events = [["INSERT","0","abc"],["DELETE","4","1"],["INSERT","1","X"]]return = "aXbc"The out-of-range delete is rejected; the final insert succeeds.
Constraints
0 <= events.length <= 20000.- Inserted text and the final document together contain at most 200000 ASCII characters.
- Numeric fields are nonnegative decimal integers.