Longest Substring of One Repeating Character - Problem

You are given a string s and will perform a series of character update operations on it. After each update, you need to find the longest substring consisting of only one repeating character.

More specifically, you receive:

  • A 0-indexed string s
  • A string queryCharacters of length k containing the new characters
  • An array queryIndices of length k containing the positions to update

For the i-th query, you update s[queryIndices[i]] to queryCharacters[i], then find the length of the longest substring where all characters are identical.

Goal: Return an array where each element represents the longest repeating substring length after each respective query.

Example: If s = "babacc" and you change s[2] to 'b', the string becomes "babbc", and the longest repeating substring is "bb" with length 2.

Input & Output

example_1.py โ€” Basic Update
$ Input: s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3]
โ€บ Output: [3, 3, 4]
๐Ÿ’ก Note: Query 1: s[1] = 'b' โ†’ "bbbacc" (longest: "bbb" = 3). Query 2: s[3] = 'c' โ†’ "bbbccc" (longest: "bbb" or "ccc" = 3). Query 3: s[3] = 'b' โ†’ "bbbbc" then check โ†’ "bbbbc" + "c" = "bbbbcc" (longest: "bbbb" = 4)
example_2.py โ€” Single Character
$ Input: s = "abcd", queryCharacters = "a", queryIndices = [0]
โ€บ Output: [1]
๐Ÿ’ก Note: Query 1: s[0] = 'a' โ†’ "abcd" (already 'a', no change, longest repeating substring is still 1)
example_3.py โ€” Creating Long Sequence
$ Input: s = "abab", queryCharacters = "aabb", queryIndices = [1,0,3,2]
โ€บ Output: [2, 3, 2, 4]
๐Ÿ’ก Note: Query 1: "aaab" (longest: "aaa" = 2). Query 2: "aaab" (longest: "aaa" = 3). Query 3: "aaab" โ†’ "aaab" (longest: 2). Query 4: "aaaa" (longest: "aaaa" = 4)

Visualization

Tap to expand
Stadium Wave ManagementStadium Sections (Characters):AAABCCWave Segments:1212After Fan Changes (Aโ†’A, same color):AAABCCNew Wave Segments (Merged!):312๐ŸŽฏ Key Insight:Instead of recounting the entire stadium after each color change,we maintain a map of wave segments and efficiently merge/split them.This reduces time complexity from O(kร—n) to O(kร—log n)!
Understanding the Visualization
1
Initial Setup
Identify all continuous segments of same-colored shirts
2
Fan Changes Color
A fan changes their shirt color, potentially splitting or merging segments
3
Update Segments
Efficiently update affected segments without checking entire stadium
4
Find Longest Wave
Query maximum segment length in logarithmic time
Key Takeaway
๐ŸŽฏ Key Insight: Maintain intervals of consecutive characters and update only affected regions instead of rescanning the entire string, achieving logarithmic time per query.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k ร— log n)

Each of k queries takes O(log n) time for segment tree operations

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for segment tree and interval tracking structures

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of lowercase English letters only
  • k == queryCharacters.length == queryIndices.length
  • 1 โ‰ค k โ‰ค 105
  • queryCharacters consists of lowercase English letters only
  • 0 โ‰ค queryIndices[i] < s.length
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen