Positions of Large Groups
EASYDescription
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 <= 1000scontains lowercase English letters only.
Approaches
Checkout 2 different approaches to solve Positions of Large Groups. Click on different approaches to view the approach and algorithm in detail.
Two-Pass Approach
This method involves two separate iterations over the data. The first pass identifies all consecutive groups of identical characters and stores their start and end indices. The second pass then filters this list to find only the "large" groups (those with a length of 3 or more).
Algorithm
-
- Initialize an empty list,
allGroups, to store the intervals of every group.
- Initialize an empty list,
-
- Iterate through the input string
susing a pointerito find the start and end of each group of identical characters.
- Iterate through the input string
-
- For each group found, starting at index
startand ending atend, add the interval[start, end]to theallGroupslist.
- For each group found, starting at index
-
- After the first pass is complete, initialize another empty list,
result.
- After the first pass is complete, initialize another empty list,
-
- Iterate through the
allGroupslist. For each interval[start, end], calculate its length (end - start + 1).
- Iterate through the
-
- If the length is 3 or greater, add the interval to the
resultlist.
- If the length is 3 or greater, add the interval to the
-
- Finally, return the
resultlist.
- Finally, return the
This approach separates the problem into two distinct steps. First, we iterate through the string to identify every single group of consecutive characters, regardless of size. We store the start and end indices of each of these groups in an intermediate list. Once we have a complete list of all groups, we perform a second pass over this new list. In this second pass, we check the size of each group and add only the ones that meet the 'large' criteria (length >= 3) to our final result list.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public List<List<Integer>> largeGroupPositions(String s) {
List<List<Integer>> allGroups = new ArrayList<>();
int n = s.length();
if (n == 0) {
return new ArrayList<>();
}
int i = 0;
while (i < n) {
int start = i;
i++;
while (i < n && s.charAt(i) == s.charAt(start)) {
i++;
}
allGroups.add(Arrays.asList(start, i - 1));
}
List<List<Integer>> result = new ArrayList<>();
for (List<Integer> group : allGroups) {
int start = group.get(0);
int end = group.get(1);
if (end - start + 1 >= 3) {
result.add(group);
}
}
return result;
}
}
Complexity Analysis
Pros and Cons
- Conceptually simple, as it separates the logic of finding groups from filtering them.
- Requires extra space to store all groups, which is less memory-efficient than a single-pass solution.
- Involves two separate loops over the data, which can be slightly slower in practice than a single loop.
Code Solutions
Checking out 3 solutions in different languages for Positions of Large Groups. Click on different languages to view the code.
class Solution {
public
List<List<Integer>> largeGroupPositions(String s) {
int n = s.length();
int i = 0;
List<List<Integer>> ans = new ArrayList<>();
while (i < n) {
int j = i;
while (j < n && s.charAt(j) == s.charAt(i)) {
++j;
}
if (j - i >= 3) {
ans.add(Arrays.asList(i, j - 1));
}
i = j;
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Positions of Large Groups
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.