Leetcode 1211 Solution

This article provides solution to leetcode question 1211 (iterator-for-combination)

https://leetcode.com/problems/iterator-for-combination

Solution

class CombinationIterator:
    def __init__(self, characters: str, combinationLength: int):
        self.chars = characters
        self.pointers = list(range(combinationLength))
        self.has_next = True

    def next(self) -> str:
        ans = "".join([
            self.chars[pointer] for pointer in self.pointers
        ])

        boundry = len(self.chars)
        index = len(self.pointers) - 1
        while index >= 0:
            if self.pointers[index] + 1 < boundry:
                break
            boundry = self.pointers[index]
            index -= 1

        if index < 0:
            self.has_next = False
        else:
            self.pointers[index] += 1

            for i in range(index + 1, len(self.pointers)):
                self.pointers[i] = self.pointers[i - 1] + 1

        return ans

    def hasNext(self) -> bool:
        return self.has_next