题目
Design an Iterator class, which has:
- A constructor that takes a string
characters
of sorted distinct lowercase English letters and a numbercombinationLength
as arguments. - A function
next()
that returns the next combination of lengthcombinationLength
in lexicographical order. - A function
hasNext()
that returnsTrue
if and only if there exists a next combination.
Example:
1 | CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator. |
Constraints:
1 <= combinationLength <= characters.length <= 15
- There will be at most
10^4
function calls per test. - It’s guaranteed that all calls of the function
next
are valid.
思路
Python built-in itertools.combinations
.
代码
1 | class CombinationIterator(object): |