857. Positions of Large Groups
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: One Pass (Two Pointers)
This problem asks us to find contiguous groups of identical characters with a length of at least 3. A single pass through the string is sufficient to identify all such groups.
Solution
Positions of Large Groups in Python
class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
result = []
n = len(s)
start = 0 # The start index of the current group
# Iterate through the string to find groups.
# We loop up to n (inclusive) to handle the last group gracefully.
for i in range(n + 1):
# A group ends if we are at the end of the string or the next character is different.
if i == n or s[i] != s[start]:
# The current group is from index `start` to `i-1`.
# Check if its length is 3 or more.
if i - start >= 3:
result.append([start, i - 1])
# Start a new group from the current position.
if i < n:
start = i
return result
Positions of Large Groups in Java
class Solution {
public List<List<Integer>> largeGroupPositions(String s) {
List<List<Integer>> result = new ArrayList<>();
int n = s.length();
int start = 0; // The start index of the current group
// Iterate through the string to find groups.
// We loop up to n (inclusive) to handle the last group gracefully.
for (int i = 0; i <= n; i++) {
// A group ends if we are at the end of the string or the next character is different.
if (i == n || s.charAt(i) != s.charAt(start)) {
// The current group is from index `start` to `i-1`.
// Check if its length is 3 or more.
if (i - start >= 3) {
result.add(Arrays.asList(start, i - 1));
}
// Start a new group from the current position.
start = i;
}
}
return result;
}
}
Positions of Large Groups in C++
class Solution {
public:
vector<vector<int>> largeGroupPositions(string s) {
vector<vector<int>> result;
int n = s.length();
int start = 0; // The start index of the current group
// Iterate through the string to find groups.
// We loop up to n (inclusive) to handle the last group gracefully.
for (int i = 0; i <= n; ++i) {
// A group ends if we are at the end of the string or the next character is different.
if (i == n || s[i] != s[start]) {
// The current group is from index `start` to `i-1`.
// Check if its length is 3 or more.
if (i - start >= 3) {
result.push_back({start, i - 1});
}
// Start a new group from the current position.
start = i;
}
}
return result;
}
};
Positions of Large Groups in TypeScript
function largeGroupPositions(s: string): number[][] {
const result: number[][] = [];
const n = s.length;
let start = 0; // The start index of the current group
// Iterate through the string to find groups.
// We loop up to n (inclusive) to handle the last group gracefully.
for (let i = 0; i <= n; i++) {
// A group ends if we are at the end of the string or the next character is different.
if (i === n || s[i] !== s[start]) {
// The current group is from index `start` to `i-1`.
// Check if its length is 3 or more.
if (i - start >= 3) {
result.push([start, i - 1]);
}
// Start a new group from the current position.
start = i;
}
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.