Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CombinationIterator:
- def __init__(self, characters: str, combinationLength: int):
- self.cs = list(characters)
- self.len = combinationLength
- self.cur = [i for i in range(self.len)]
- def next(self) -> str:
- result = "".join(self.cs[i] for i in self.cur)
- max_allowed_idx = len(self.cs) - 1
- while self.cur:
- self.cur[-1] += 1
- if self.cur[-1] <= max_allowed_idx:
- break
- self.cur.pop()
- max_allowed_idx -= 1
- if not self.cur:
- self.cur = None
- else:
- while len(self.cur) < self.len:
- self.cur.append(self.cur[-1] + 1)
- return result
- def hasNext(self) -> bool:
- return self.cur is not None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement