Positions of Large Groups

You can find the intervals of large groups in the given string by iterating through the string and checking the length of consecutive characters. If the length is 3 or more, you can mark it as a large group.

Here’s the solution:

  1. Initialize a list to store the large groups’ intervals.
  2. Iterate through the string using a loop, keeping track of the start and end index of consecutive characters.
  3. When a new character is encountered, check if the current group’s length is 3 or more. If so, add the interval to the list.
  4. Continue this process until the end of the string.

Here’s the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution:
    def largeGroupPositions(self, s: str) -> List[List[int]]:
        result = []  # To store the intervals of large groups
        start = 0    # To keep track of the start index of the current group

        # Iterate through the string and check for consecutive characters
        for i in range(1, len(s)):
            if s[i] != s[i - 1]: # New character encountered
                if i - start >= 3:  # Check if the current group is large
                    result.append([start, i - 1])  # Add interval to result
                start = i  # Update the start index for the next group

        # Check for the last group in the string
        if len(s) - start >= 3:
            result.append([start, len(s) - 1])

        return result

This solution iterates through the string once, so the time complexity is O(n), where n is the length of the string. It returns the intervals of every large group sorted in increasing order by the start index.