{x}
blog image

Positions of Large Groups

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.

 

Example 1:

Input: s = "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the only large group with start index 3 and end index 6.

Example 2:

Input: s = "abc"
Output: []
Explanation: We have groups "a", "b", and "c", none of which are large groups.

Example 3:

Input: s = "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
Explanation: The large groups are "ddd", "eeee", and "bbb".

 

Constraints:

  • 1 <= s.length <= 1000
  • s contains lowercase English letters only.

Solution Explanation: Positions of Large Groups

The problem asks to find all "large groups" within a string and return their starting and ending indices. A large group is defined as a consecutive sequence of 3 or more identical characters.

Approach: Two Pointers

The most efficient approach uses two pointers, i and j, to iterate through the string.

  • i points to the beginning of a group.
  • j iterates to find the end of the group.

The algorithm proceeds as follows:

  1. Initialization: i is initialized to 0. An empty list ans is created to store the results (intervals of large groups).

  2. Iterate through groups: The outer loop continues as long as i is within the string bounds.

  3. Find group end: The inner loop increments j while the character at j is the same as the character at i. This effectively finds the end of the current group.

  4. Check group size: After the inner loop, j - i represents the length of the group. If this length is 3 or greater, the interval [i, j - 1] is added to the ans list.

  5. Update i: i is updated to j, moving to the beginning of the next group.

Time and Space Complexity

  • Time Complexity: O(n), where n is the length of the string. The algorithm iterates through the string at most twice (once with i and once with j).

  • Space Complexity: O(k), where k is the number of large groups. In the worst case, k could be proportional to n (e.g., a string with alternating large groups), but generally, it is much smaller. The space used is primarily for storing the results in ans.

Code Implementation (Python)

class Solution:
    def largeGroupPositions(self, s: str) -> List[List[int]]:
        i, n = 0, len(s)
        ans = []
        while i < n:
            j = i
            while j < n and s[j] == s[i]:
                j += 1
            if j - i >= 3:
                ans.append([i, j - 1])
            i = j
        return ans
 

The code in other languages (Java, C++, Go, TypeScript) follows the same logic with only syntactic differences to reflect the specific language features. The core algorithm remains identical and efficient.