선발대

[스파르타] 99클럽 2기 코테스터디 29일차 TIL / leet 1286 본문

스파르타코딩클럽/활동 내용

[스파르타] 99클럽 2기 코테스터디 29일차 TIL / leet 1286

신선한 스타트 2024. 6. 17. 20:12

Leetcode: 1286. Iterator for Combination (링크)

 

 

class CombinationIterator:

    def __init__(self, characters: str, combinationLength: int):
        self.combos = []
        self.idx = 0

        def helper(string, idx):
            if len(string) == combinationLength:
                self.combos.append(string)
                return
            else:
                for i in range(idx, len(characters)):
                    helper(string + characters[i], i + 1)
        
        helper('', 0)

    def next(self) -> str:
        self.idx += 1
        return self.combos[self.idx - 1]

    def hasNext(self) -> bool:
        return self.idx < len(self.combos)


# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()

오늘 미들러 풀이 끝!

 


참고한 풀이: https://www.youtube.com/watch?v=fidR9XIBaC4

Comments