You’re on the infrastructure team for a fintech customer-support platform with millions of daily active agents. To meet audit and compliance requirements, every edit an agent makes to a chat transcript is stored as a compact diff log (insert/delete operations). During investigations, you must replay these logs to reconstruct the exact final transcript.
Replaying edits naively with Python string slicing can become too slow when transcripts and logs are large. Your task is to implement a function that applies a sequence of operations to an initial string.
You are given:
base: str — the initial documentops: List[str] — a list of operations, applied in orderEach operation is one of:
Insert: "I <pos> <text>"
<text> starting at character index pos.<text> may contain spaces and must be preserved exactly (including leading spaces).Delete: "D <pos> <len>"
<len> characters starting at index pos.All operations are guaranteed valid for the document state at the time they are applied.
Return the final document string after applying all operations.
Example 1
base = "hello world", ops = ["I 5 ,", "D 6 5", "I 6 React"]"hello, React""," at index 5 → "hello, world""world") → "hello, ""React" at index 6 → "hello, React"Example 2
Input: base = "12345", ops = ["I 5 6", "D 2 3", "I 2 abc"]
Output: "12abc56"
Explanation:
"6" at index 5 → "123456""345") → "126""abc" at index 2 → "12abc6"Note: indices are always applied to the current document; the correct final string is "12abc6" only if step (1) inserted at the end of the original string. Here it inserts into the current document, producing "12abc56".
0 <= len(base) <= 2 * 10^51 <= len(ops) <= 2 * 10^5sum(len(text_i)) <= 2 * 10^5