-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1286.py
More file actions
63 lines (48 loc) · 2.12 KB
/
1286.py
File metadata and controls
63 lines (48 loc) · 2.12 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
__________________________________________________________________________________________________
sample 32 ms submission
from itertools import combinations
class CombinationIterator:
def __init__(self, characters: str, combinationLength: int):
generator = list(map("".join, combinations(characters, combinationLength)))[::-1]
self.next = generator.pop
self.hasNext = lambda: bool(generator)
# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()
__________________________________________________________________________________________________
sample 36 ms submission
import collections
class CombinationIterator:
def __init__(self, characters: str, combinationLength: int):
self.chars = characters
self.combLen = combinationLength
self.queue = collections.deque(itertools.combinations(self.chars, self.combLen))
def next(self) -> str:
return ''.join(self.queue.popleft())
def hasNext(self) -> bool:
return bool(self.queue)
# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()
__________________________________________________________________________________________________
sample 40 ms submission
from itertools import combinations
class CombinationIterator:
def __init__(self, characters: str, combinationLength: int):
self.it = combinations(characters, combinationLength)
self.buffer = "".join(next(self.it)) if characters else None
def next(self) -> str:
res = self.buffer
try:
self.buffer = "".join(next(self.it))
except:
self.buffer = None
return res
def hasNext(self) -> bool:
return self.buffer is not None
# Your CombinationIterator object will be instantiated and called as such:
# obj = CombinationIterator(characters, combinationLength)
# param_1 = obj.next()
# param_2 = obj.hasNext()