Skip to content

Practice Iterator for Combination

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1286
LeetCode Problem

Iterator for Combination

Problem Statement

CombinationIterator(characters,combinationLength) receives sorted distinct lowercase letters. next() returns the next fixed-length combination in lexicographic order. hasNext() reports whether another combination remains. Calls to next are valid. Practice approach: Maintain increasing selected indices. To advance, move the rightmost index that still has room and reset later indices to consecutive successors. This generates combinations in order without permutations or duplicates. Keep exhaustion separate from the current value. Test length one and selecting all characters.

Source: Iterator for Combination on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["CombinationIterator","next","next","hasNext","next","hasNext"]
[["acd",2],[],[],[],[],[]]
Output
[null,"ac","ad",true,"cd",false]
Explanation

The two-letter combinations are ac, ad, and cd in lexical order.

Constraints

1 <= combinationLength <= characters.length <= 15
Characters are distinct lowercase letters in sorted order.
At most 10^4 total next and hasNext calls.
All next calls are valid.

Related LeetCode Problems

Practice more problems similar to Iterator for Combination.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.