Longest Equal-Character Run After Each Update

Quick Overview

Longest Equal-Character Run After Each Update. Starting from lowercase string s, process queries in order: query i replaces s[queryIndices[i]] by queryCharacters[i]. Return one number per query giving the length of the longest contiguous run of a single character after that update. Updates persist for subsequent queries.

Longest Equal-Character Run After Each Update

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem You are given a lowercase string `s` and a sequence of updates. Update `i` replaces the character at zero-based index `queryIndices[i]` with `queryCharacters[i]`. Apply updates in order, retaining each change for later updates. Return one length per update: the longest contiguous run of a single repeated character in the whole string after that change. ```cpp class Solution { public: vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices); }; ``` ### Examples **Example 1:** ```text s = "aabb", queryCharacters = "ab", queryIndices = [2,1] Output: [3,1] Explanation: The strings become "aaab" with a run of three a characters, then "abab" with no repeated adjacent characters. ``` **Example 2:** ```text s = "abc", queryCharacters = "aa", queryIndices = [1,2] Output: [2,3] Explanation: The strings become "aac" and then "aaa". ``` ### Constraints - `1 <= s.length <= 10^5`; s is lowercase. - `1 <= queryCharacters.length == queryIndices.length <= 10^5`. - Query characters are lowercase and every index is within s. ### Clarifying Questions Even an update that leaves a character unchanged still produces an answer. Return run lengths, not the substring itself. ```hint Inspect the structure Which summary fields allow the longest run of two adjacent segments to be merged? ``` ### Reasoning checkpoints - Apply the updates sequentially and report an answer after every one, even if a character does not change. - Count contiguous equal-character runs, including runs that join or split at an updated position. - Support the maximum string and query sizes and justify the claimed update cost. ### Optional extensions Could you also answer longest-run queries for a specified subinterval of the current string?

Overview: Longest Equal-Character Run After Each Update. Starting from lowercase string s, process queries in order: query i replaces s[queryIndices[i]] by queryCharacters[i]. Return one number per query giving the length of the longest contiguous run of a single character after that update. Updates persist for subsequent queries.

Google logo
Google
Aug 7, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

You are given a lowercase string s and a sequence of updates. Update i replaces the character at zero-based index queryIndices[i] with queryCharacters[i]. Apply updates in order, retaining each change for later updates. Return one length per update: the longest contiguous run of a single repeated character in the whole string after that change.

class Solution {
public:
    vector<int> longestRepeating(string s, string queryCharacters, vector<int>& queryIndices);
};

Examples

Example 1:

s = "aabb", queryCharacters = "ab", queryIndices = [2,1]
Output: [3,1]
Explanation: The strings become "aaab" with a run of three a characters, then "abab" with no repeated adjacent characters.

Example 2:

s = "abc", queryCharacters = "aa", queryIndices = [1,2]
Output: [2,3]
Explanation: The strings become "aac" and then "aaa".

Constraints

  • 1 <= s.length <= 10^5 ; s is lowercase.
  • 1 <= queryCharacters.length == queryIndices.length <= 10^5 .
  • Query characters are lowercase and every index is within s.

Clarifying Questions Guidance

Even an update that leaves a character unchanged still produces an answer. Return run lengths, not the substring itself.

Reasoning checkpoints

  • Apply the updates sequentially and report an answer after every one, even if a character does not change.
  • Count contiguous equal-character runs, including runs that join or split at an updated position.
  • Support the maximum string and query sizes and justify the claimed update cost.

Optional extensions

Could you also answer longest-run queries for a specified subinterval of the current string?