-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
26 lines (22 loc) · 867 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class CombinationIterator:
def __init__(self, characters: str, combinationLength: int):
self.curr = (1 << len(characters)) - 1
self.size = combinationLength
self.cs = characters[::-1]
def next(self) -> str:
while self.curr >= 0 and self.curr.bit_count() != self.size:
self.curr -= 1
ans = []
for i in range(len(self.cs)):
if (self.curr >> i) & 1:
ans.append(self.cs[i])
self.curr -= 1
return ''.join(ans[::-1])
def hasNext(self) -> bool:
while self.curr >= 0 and self.curr.bit_count() != self.size:
self.curr -= 1
return self.curr >= 0
# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()