Positions of Large Groups - Problem

Imagine you're analyzing patterns in a string of characters! Given a string s of lowercase letters, characters naturally form consecutive groups of the same character.

For example, in the string "abbxxxxzyy", we can identify these groups:
"a" at position [0,0]
"bb" at positions [1,2]
"xxxx" at positions [3,6]
"z" at position [7,7]
"yy" at positions [8,9]

A group is considered "large" if it contains 3 or more characters. Your task is to find all large groups and return their [start, end] intervals (inclusive) sorted by start index.

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

Input & Output

example_1.py — Basic Case
$ Input: s = "abbxxxxzyy"
Output: [[3,6]]
💡 Note: The groups are "a", "bb", "xxxx", "z", "yy". Only "xxxx" has 3+ characters, spanning indices 3 to 6.
example_2.py — Multiple Large Groups
$ Input: s = "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
💡 Note: Large groups are "ddd" at [3,5], "eeee" at [6,9], and "bbb" at [12,14].
example_3.py — No Large Groups
$ Input: s = "aba"
Output: []
💡 Note: All groups ("a", "b", "a") have size 1, so no large groups exist.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s contains only lowercase English letters
  • Groups are formed by consecutive identical characters

Visualization

Tap to expand
Positions of Large Groups - Visual SolutionInput: "abbxxxxzyy"a0b1b2x3x4x5x6z7y8y9Large Group: [3,6]Group Analysis:"a" - Size: 1 (too small)"bb" - Size: 2 (too small)"xxxx" - Size: 4 ≥ 3 ✓ LARGE"z" - Size: 1 (too small)"yy" - Size: 2 (too small)Result: [[3,6]]
Understanding the Visualization
1
Scan String
Move through string character by character
2
Track Groups
Keep count of consecutive identical characters
3
Identify Large Groups
Mark groups with 3+ characters
4
Record Positions
Store start and end indices of large groups
Key Takeaway
🎯 Key Insight: Use a single pass with two pointers to efficiently identify consecutive character groups and check their sizes - no need for complex data structures!
Asked in
Google 45 Amazon 32 Facebook 28 Microsoft 21
28.4K Views
Medium Frequency
~15 min Avg. Time
890 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