Sponsored
Sponsored
This approach involves iterating through the string to detect groups of consecutive characters. We maintain a start index for the current group and check whenever a character changes. If the length of a group is 3 or more, we record the start and end indices.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n) for storing the result.
1
2def large_group_positions(s):
3 result = []
4 start = 0
5 for i in range(1, len(s) + 1):
6 if i == len(s) or s[i] != s[start]:
7 if i - start >= 3:
8 result.append([start, i - 1])
9 start = i
10 return result
11
In this Python solution, we utilize list operations to determine the start and end of each group. If a group is determined to be large, its indices are appended to the result list.
The two-pointer technique is another efficient way to solve this problem. The idea is to have a slow pointer marking the start of the group and a fast pointer iterating through the string. When the character at the fast pointer changes or reaches the end, check the length of the group.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(n) for storing the result.
1
The Python solution uses a loop with two variables representing `slow` and `fast` pointers. As `fast` checks each character, changes or end conditions trigger evaluations.