Advance an end pointer over each maximal run, record qualifying bounds, and continue from the next character.
Complexity
O(n) time and O(1) auxiliary space excluding output.
Return the start and end indices of every repeated-character group of length at least three.
Advance an end pointer over each maximal run, record qualifying bounds, and continue from the next character.
O(n) time and O(1) auxiliary space excluding output.
package main
func largeGroupPositions(s string) [][]int {
result := [][]int{}
for start := 0; start < len(s); {
end := start + 1
for end < len(s) && s[end] == s[start] {
end++
}
if end-start >= 3 {
result = append(result, []int{start, end - 1})
}
start = end
}
return result
}
class Solution:
def largeGroupPositions(self, s: str) -> list[list[int]]:
result: list[list[int]] = []
start = 0
while start < len(s):
end = start + 1
while end < len(s) and s[end] == s[start]: end += 1
if end - start >= 3: result.append([start, end - 1])
start = end
return result
function largeGroupPositions(s: string): number[][] {
const result: number[][] = [];
let start = 0;
while (start < s.length) { let end = start + 1; while (end < s.length && s[end] === s[start]) end += 1; if (end - start >= 3) result.push([start, end - 1]); start = end; }
return result;
}