본문 바로가기
Problem Solving/LeetCode

[LeetCode] 830. Positions of Large Groups - Java

by graycode 2026. 10. 23.

• 문제 링크

 

Positions of Large Groups - LeetCode

Can you solve this real interview question? Positions of Large Groups - 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

leetcode.com

 

• 풀이 코드

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public List<List<Integer>> largeGroupPositions(String s) {
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0, j = 0, n = s.length(); i < n; i++) {
            if (i == n - 1 || s.charAt(i) != s.charAt(i + 1)) {
                if (i - j + 1 >= 3) res.add(List.of(j, i));
                j = i + 1;
            }
        }

        return res;
    }

}

댓글