You are given a 0-indexed string s. You are also given a 0-indexed string queryCharacters of length k and a 0-indexed array of integer indicesqueryIndices of length k, both of which are used to describe k queries.
The ith query updates the character in s at index queryIndices[i] to the character queryCharacters[i].
Return an arraylengthsof lengthkwherelengths[i]is the length of the longest substring ofsconsisting of only one repeating character after theithqueryis performed.
Input:
s ="babacc", queryCharacters ="bcb", queryIndices =[1,3,3]Output:
[3,3,4]Explanation:
-1st query updates s ="b**b**bacc". The longest substring consisting of one repeating character is"bbb"with length 3.-2nd query updates s ="bbb**c**cc". The longest substring consisting of one repeating character can be "bbb" or "ccc"with length 3.-3rd query updates s ="bbb**b**cc". The longest substring consisting of one repeating character is"bbbb"with length 4.Thus, we return[3,3,4].
Example 2:
1
2
3
4
5
6
7
8
Input:
s ="abyzz", queryCharacters ="aa", queryIndices =[2,1]Output:
[2,3]Explanation:
-1st query updates s ="ab**a**zz". The longest substring consisting of one repeating character is"zz"with length 2.-2nd query updates s ="a**a**azz". The longest substring consisting of one repeating character is"aaa"with length 3.Thus, we return[2,3].
This problem requires tracking and updating the longest consecutive substring of identical characters in s after every query. Each query modifies a character, which affects the longest substring.
⏰ Time complexity: O(n + k) where n is the length of s and k is the number of queries. Calculating the initial substring lengths involves O(n), and processing each query takes O(1) as adjustments are local.
🧺 Space complexity: O(n) due to storage for tracking substrings.