Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2213: Longest Substring of One Repeating Character

In this guide, we solve Leetcode #2213 Longest Substring of One Repeating Character in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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 indices queryIndices of length k, both of which are used to describe k queries.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Segment Tree, Array, String, Ordered Set

Intuition

We need to scan characters while tracking positions or counts.

A simple state machine keeps the logic precise.

Approach

Iterate through the string once and update the state for each character.

Use a map or array if you need fast lookups.

Steps:

  • Iterate through characters.
  • Maintain necessary state.
  • Build or validate the output.

Example

Input: s = "babacc", queryCharacters = "bcb", queryIndices = [1,3,3] Output: [3,3,4] Explanation: - 1st query updates s = "bbbacc". The longest substring consisting of one repeating character is "bbb" with length 3. - 2nd query updates s = "bbbccc". The longest substring consisting of one repeating character can be "bbb" or "ccc" with length 3. - 3rd query updates s = "bbbbcc". The longest substring consisting of one repeating character is "bbbb" with length 4. Thus, we return [3,3,4].

Python Solution

def max(a: int, b: int) -> int: return a if a > b else b class Node: __slots__ = "l", "r", "lmx", "rmx", "mx" def __init__(self, l: int, r: int): self.l = l self.r = r self.lmx = self.rmx = self.mx = 1 class SegmentTree: __slots__ = "s", "tr" def __init__(self, s: str): self.s = list(s) n = len(s) self.tr: List[Node | None] = [None] * (n * 4) self.build(1, 1, n) def build(self, u: int, l: int, r: int): self.tr[u] = Node(l, r) if l == r: return mid = (l + r) // 2 self.build(u << 1, l, mid) self.build(u << 1 | 1, mid + 1, r) self.pushup(u) def query(self, u: int, l: int, r: int) -> int: if self.tr[u].l >= l and self.tr[u].r <= r: return self.tr[u].mx mid = (self.tr[u].l + self.tr[u].r) // 2 ans = 0 if r <= mid: ans = self.query(u << 1, l, r) if l > mid: ans = max(ans, self.query(u << 1 | 1, l, r)) return ans def modify(self, u: int, x: int, v: str): if self.tr[u].l == self.tr[u].r: self.s[x - 1] = v return mid = (self.tr[u].l + self.tr[u].r) // 2 if x <= mid: self.modify(u << 1, x, v) else: self.modify(u << 1 | 1, x, v) self.pushup(u) def pushup(self, u: int): root, left, right = self.tr[u], self.tr[u << 1], self.tr[u << 1 | 1] root.lmx = left.lmx root.rmx = right.rmx root.mx = max(left.mx, right.mx) a, b = left.r - left.l + 1, right.r - right.l + 1 if self.s[left.r - 1] == self.s[right.l - 1]: if left.lmx == a: root.lmx += right.lmx if right.rmx == b: root.rmx += left.rmx root.mx = max(root.mx, left.rmx + right.lmx) class Solution: def longestRepeating( self, s: str, queryCharacters: str, queryIndices: List[int] ) -> List[int]: tree = SegmentTree(s) ans = [] for x, v in zip(queryIndices, queryCharacters): tree.modify(1, x + 1, v) ans.append(tree.query(1, 1, len(s))) return ans

Complexity

The time complexity is O(n×log⁡n)O(n \times \log n)O(n×logn), and the space complexity is O(n×log⁡n)O(n \times \log n)O(n×logn). The space complexity is O(n×log⁡n)O(n \times \log n)O(n×logn).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy