中等
请你设计一个迭代器类 CombinationIterator ,包括以下内容:
CombinationIterator(string characters, int combinationLength)一个构造函数,输入参数包括:用一个 有序且字符唯一 的字符串characters(该字符串只包含小写英文字母)和一个数字combinationLength。- 函数
next(),按 字典序 返回长度为combinationLength的下一个字母组合。 - 函数
hasNext(),只有存在长度为combinationLength的下一个字母组合时,才返回true
示例 1:
输入:
["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
[["abc", 2], [], [], [], [], [], []]
输出:
[null, "ab", true, "ac", true, "bc", false]
解释:
CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
iterator.next(); // 返回 "ab"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "ac"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "bc"
iterator.hasNext(); // 返回 false
提示:
1 <= combinationLength <= characters.length <= 15-
characters中每个字符都 不同 - 每组测试数据最多对
next和hasNext调用104次 - 题目保证每次调用函数
next时都存在下一个字母组合。