File: positions-of-large-groups/solution.py

Date: 2026-06-06

Time: 18:33

Purpose

This file solves LeetCode 830 — Positions of Large Groups. It identifies contiguous runs of the same character in a string where the run length is 3 or more, returning the [start, end] index pair for each such group.

It's one solution module among hundreds in the leetcode-implementations repo, following the standard solution.py + test_solution.py + plan.md + review.md structure.

Key Components

Solution.largeGroupPositions(self, s: str) -> List[List[int]] — the single method. Takes a lowercase string, returns a list of [start, end] intervals (inclusive on both ends) for every group of 3+ consecutive identical characters.

Patterns

Single-pass group detection — the algorithm tracks the start of the current run and advances i until the character changes (or the string ends). When a boundary is hit, it checks whether the group is "large" (length >= 3) and records it. This is the idiomatic way to find consecutive runs in O(n) time and O(1) extra space (excluding the result list).

Sentinel-style loop bound — the loop runs to len(s) + 1 (not len(s)), and the condition i == len(s) acts as a virtual sentinel to flush the final group. This avoids duplicating the "emit group" logic after the loop — a common pattern in run-length problems.

Dependencies

Imports: List from typing — used only for the return type annotation.

Imported by: positions-of-large-groups/test_solution.py imports the Solution class. The large "Imported By" list in the prompt is noise from the repo-wide import graph of typing.List, not actual dependents of this module.

Flow

1. Initialize result = [] and start = 0 (beginning of the first group).

2. Iterate i from 1 through len(s) inclusive.

3. At each i, if i is past the end or s[i] differs from s[start], the current group [start, i-1] has ended.

4. If the group length i - start >= 3, append [start, i - 1] to result.

5. Reset start = i to begin the next group.

6. Return result.

For input "aaa": i=1 same, i=2 same, i=3 triggers i == len(s), group length 3 >= 3, emits [0, 2].

Invariants

Error Handling

None. The method assumes a valid lowercase string per the LeetCode contract. An empty string produces an empty result without error.

Topics to Explore

Beliefs