题目
Design an Iterator class, which has:
- A constructor that takes a string
charactersof sorted distinct lowercase English letters and a numbercombinationLengthas arguments. - A function
next()that returns the next combination of lengthcombinationLengthin lexicographical order. - A function
hasNext()that returnsTrueif 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^4function calls per test. - It’s guaranteed that all calls of the function
nextare valid.
思路
Python built-in itertools.combinations.
代码
1 | class CombinationIterator(object): |