Positions of Large Groups - Problem

In a string s of lowercase letters, these letters form consecutive groups of the same character.

For example, a string like s = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z", and "yy".

A group is identified by an interval [start, end], where start and end denote the start and end indices (inclusive) of the group. In the above example, "xxxx" has the interval [3,6].

A group is considered large if it has 3 or more characters.

Return the intervals of every large group sorted in increasing order by start index.

Input & Output

Example 1 — Basic Case
$ Input: s = "abbxxxxzyy"
Output: [[3,6]]
💡 Note: The groups are "a" (length 1), "bb" (length 2), "xxxx" (length 4), "z" (length 1), "yy" (length 2). Only "xxxx" has length ≥ 3, spanning indices [3,6].
Example 2 — Multiple Large Groups
$ Input: s = "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
💡 Note: Groups: "a"(1), "b"(1), "c"(1), "ddd"(3), "eeee"(4), "aa"(2), "bbb"(3), "c"(1), "d"(1). Large groups are "ddd" [3,5], "eeee" [6,9], "bbb" [12,14].
Example 3 — No Large Groups
$ Input: s = "abc"
Output: []
💡 Note: All groups have length 1, which is less than 3, so no large groups exist.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters only

Visualization

Tap to expand
Positions of Large Groups INPUT s = "abbxxxxzyy" 0 1 2 3 4 5 6 7 8 9 a b b x x x x z y y Groups Identified: "a" len=1 "bb" len=2 "xxxx" len=4 "z" len=1 "yy" len=2 Green = Large Group (3+ chars) s = "abbxxxxzyy" ALGORITHM STEPS Single Pass Scan 1 Initialize start=0, result=[] 2 Scan String Compare s[i] with s[i+1] for i from 0 to n-1 3 Group End Check If s[i] != s[i+1] or last: Group ends at index i 4 Check Large If (i - start + 1) >= 3: Add [start, i] to result Update start = i + 1 Scan Progress: i=6: 'x' != 'z' 6-3+1=4 >= 3 [OK] FINAL RESULT Large Groups Found: "xxxx" Index: [3, 6] Length: 4 chars Position in string: a b b x x x x z [3, 6] Output: [[3, 6]] [OK] 1 large group found Key Insight: Single pass O(n) scan: Track the start of each group. When character changes (s[i] != s[i+1]), the current group ends at index i. If group length (i - start + 1) is 3 or more, record [start, i]. Reset start to i+1 for the next group. This avoids nested loops and processes each character once. TutorialsPoint - Positions of Large Groups | Single Pass Scan Approach
Asked in
Google 12 Facebook 8
15.4K Views
Medium Frequency
~15 min Avg. Time
485 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