[LeetCode] 830. Positions of Large Groups (Easy) (JAVA) One question per day

【LeetCode】830. Positions of Large Groups (Easy) (JAVA)

Title address: https://leetcode.com/problems/positions-of-large-groups/

Title description:

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".

Example 4:

Input: s = "aba"
Output: []

Constraints:

  • 1 <= s.length <= 1000
  • s contains lower-case English letters only.

General idea

In a string s composed of lowercase letters, it contains a group of consecutive identical characters.

For example, in the string s = "abbxxxxzyy", there are some groups such as "a", "bb", "xxxx", "z" and "yy".

The grouping can be represented by the interval [start, end], where start and end respectively represent the subscripts of the start and end positions of the group. The "xxxx" grouping in the above example is represented by the interval [3,6].

We call all groups that contain more than or equal to three consecutive characters as larger groups.

Find each larger grouping interval, sort by the starting position subscript in ascending order, and return the result.

Problem-solving method

  1. First locate a different char value, and then look for it until it is different from the current char value
  2. If the difference between the ending position and the starting position is greater than or equal to 3, save the result
class Solution {
    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            int end = i + 1;
            while (end < s.length() && s.charAt(end) == cur) {
                end++;
            }
            if (end - i >= 3) {
                List<Integer> temp = new ArrayList<>();
                temp.add(i);
                temp.add(end - 1);
                list.add(temp);
            }
            i = end - 1;
        }
        return list;
    }
}

Execution time: 1 ms, beating 100.00% of Java users
Memory consumption: 38.6 MB, beating 68.86% of Java users

Welcome to pay attention to my official account, LeetCode updates one question every day

Guess you like

Origin blog.csdn.net/qq_16927853/article/details/112212246