Ctrl K

Longest Run After Each Edit

Hard
You are given a string text of lowercase English letters. Edits are then applied to it one after another: edit number i writes the letter letters[i] at position positions[i] (0-based), replacing whatever letter was there. Edits stack up: each one applies to the text as the previous edits left it. A *run* is a stretch of consecutive equal letters. After each edit, find the length of the longest run anywhere in the text. Return the list of those lengths, one per edit, in order.
Example 1
text="xxyzz", letters="zyz", positions=[2, 1, 1] → [3, 3, 4]

After the first edit the text is xxzzz and the longest run is zzz, length 3. After the second it is xyzzz, still 3. After the third it is xzzzz, a run of four z.

Example 2
text="aaaa", letters="b", positions=[1] → [2]

The text becomes abaa, and the longest run is the two a at the end.

Example 3
text="q", letters="qr", positions=[0, 0] → [1, 1]

The first edit writes the same letter and changes nothing, the second replaces the only letter. Either way the run has length 1.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 1 ≤ len(letters) ≤ 10⁵
  • len(positions) = len(letters)
  • 0 ≤ positions[i] < len(text)
  • text and letters consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCity Block Outline HardSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardA Dark Gap Between Street Lamps Hard
loading Python…
Input
text="xxyzz", letters="zyz", positions=[2, 1, 1]
Expected
[3, 3, 4]

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.