In a string S of lowercase letters, these letters form consecutive groups of the same character.

For example, a string likeS = "abbxxxxzyy"has the groups"a","bb","xxxx","z"and "yy".

Call a group_large_if it has 3 or more characters. We would like the starting and ending positions of every large group.

The final answer should be in lexicographic order.

class Solution {
    public List<List<Integer>> largeGroupPositions(String S) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(S == null || S.length() < 3)
            return res;

        int i = 0;
        int j = 0;
        int len = S.length();

        while(j < len) {
            while(j < len && S.charAt(j) == S.charAt(i))
                j++;
            if(j - i >= 3) {
                List<Integer> line = new ArrayList<Integer>();
                line.add(i);
                line.add(j - 1);
                res.add(line);
            }
            i = j;
        }

        return res;
    }
}

results matching ""

    No results matching ""