99클럽 코테 스터디 20일차 TIL [LeetCode] Iterator for Combination (Java)

민경·2024년 6월 17일

문제

[LeetCode] Iterator for Combination

풀이

정답 코드

class CombinationIterator {
    String characters;
    int combinationLength;
    List<String> combinations;
    int currentIndex;

    public CombinationIterator(String characters, int combinationLength) {
        this.characters = characters;
        this.combinationLength = combinationLength;
        this.combinations = new ArrayList<>();
        this.currentIndex = 0;
        makeCombinations(0, new StringBuilder());
    }

    private void makeCombinations(int start, StringBuilder sb) {
        if(sb.length() == combinationLength) {
            combinations.add(sb.toString());
            return;
        }
        for(int i = start; i < characters.length(); i++) {
            sb.append(characters.charAt(i));
            makeCombinations(i+1, sb);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
    
    public String next() {
        return combinations.get(currentIndex++);
    }
    
    public boolean hasNext() {
        return currentIndex < combinations.size();
    }
}

다른 풀이

profile
강해져야지

0개의 댓글