Problem Description
Given a string of lowercase letters, consecutive identical characters form groups. A group is defined by its starting and ending indices. The task is to identify all "large groups," which are groups that contain at least 3 characters, and return their intervals sorted by the start index.
Key Insights
- Traverse the string once, using a pointer to mark the start of the current group.
- When the current character differs from the previous one, determine if the group length (i - group_start) is at least 3.
- If it is, record the interval [group_start, i - 1].
- Remember to check the final group after the loop ends.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string, as we scan the string once.
Space Complexity: O(1) extra space (excluding the output list), since only a few variables are used.
Solution
The solution iterates through the string while keeping track of the start index of each consecutive group. Each time a new character is encountered, it checks if the previous group length meets or exceeds 3. If so, it records the interval. The algorithm also handles the final group after the loop concludes.