leetcode

Positions of Large Groups - Link

Question Description

In a string s of lowercase letters, a group is a consecutive run of the same character. A large group is defined as a group that has length >= 3.

Return the intervals [start, end] of every large group sorted in increasing order of start index.


Constraints


Approach

Use the two-pointer technique to identify groups of consecutive identical characters and record intervals for groups with length >= 3.

Algorithm:

  1. Initialize start pointer at index 0
  2. Iterate i from 1 to string length
  3. When we reach the end OR current character differs from character at start:
    • Calculate group length = i - start
    • If length >= 3, record interval [start, i-1]
    • Update start = i for new group
  4. Return list of all large group intervals

This approach works because:


Dry Run

Example Input: s = "abbxxxxzzy"

Step-by-step execution:

Result: [[3,6]]


Solution

class Solution {
    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> ans = new ArrayList<>();
        int n = s.length();
        int start = 0;

        for (int i = 1; i <= n; i++) {
            if (i == n || s.charAt(i) != s.charAt(start)) {
                if (i - start >= 3) {
                    ans.add(Arrays.asList(start, i - 1));
                }
                start = i;
            }
        }

        return ans;
    }
}

Time and Space Complexity


Alternative Approaches

Brute Force Approach (Less Efficient)

class Solution {
    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> result = new ArrayList<>();

        for (int i = 0; i < s.length(); ) {
            int j = i;
            // Find end of current group
            while (j < s.length() && s.charAt(j) == s.charAt(i)) {
                j++;
            }

            // Check if group size >= 3
            if (j - i >= 3) {
                result.add(Arrays.asList(i, j - 1));
            }

            i = j;
        }

        return result;
    }
}

Explanation: Use a single index that moves to find group boundaries.

Time Complexity: O(n) - still single pass, but slightly less efficient due to inner while loop Space Complexity: O(1) - constant extra space

Regex Approach

class Solution {
    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> result = new ArrayList<>();
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(.)\\1{2,}");
        java.util.regex.Matcher matcher = pattern.matcher(s);

        while (matcher.find()) {
            result.add(Arrays.asList(matcher.start(), matcher.end() - 1));
        }

        return result;
    }
}

Explanation: Use regex to find sequences of 3 or more identical characters.

Time Complexity: O(n) - regex matching is generally O(n) Space Complexity: O(1) - excluding output list