Iterator for Combination - Problem
Design a Custom Iterator for String Combinations
You need to implement a
Your class should support:
•
•
•
For example, with characters
This is a classic iterator pattern problem that tests your understanding of combinations, backtracking, and efficient state management.
You need to implement a
CombinationIterator class that generates all possible combinations of a given length from a sorted string of distinct characters. The combinations should be returned in lexicographical order.Your class should support:
•
CombinationIterator(characters, combinationLength) - Initialize with a sorted string and target length•
next() - Return the next combination in lexicographical order•
hasNext() - Check if more combinations existFor example, with characters
"abc" and length 2, you should generate: "ab", "ac", "bc" in that exact order.This is a classic iterator pattern problem that tests your understanding of combinations, backtracking, and efficient state management.
Input & Output
example_1.py — Basic Usage
$
Input:
CombinationIterator iterator = new CombinationIterator("abc", 2);
iterator.next();
iterator.hasNext();
iterator.next();
iterator.hasNext();
iterator.next();
iterator.hasNext();
›
Output:
"ab"
true
"ac"
true
"bc"
false
💡 Note:
Starting with "abc" and length 2, we generate combinations in lexicographical order: "ab", "ac", "bc". After returning all combinations, hasNext() returns false.
example_2.py — Single Character Combinations
$
Input:
CombinationIterator iterator = new CombinationIterator("abcd", 1);
iterator.next();
iterator.next();
iterator.next();
iterator.next();
iterator.hasNext();
›
Output:
"a"
"b"
"c"
"d"
false
💡 Note:
With combination length 1, each character forms its own combination. We get "a", "b", "c", "d" in order.
example_3.py — Maximum Length
$
Input:
CombinationIterator iterator = new CombinationIterator("abc", 3);
iterator.next();
iterator.hasNext();
›
Output:
"abc"
false
💡 Note:
When combination length equals string length, there's only one possible combination: the entire string. After returning it, no more combinations exist.
Constraints
- 1 ≤ combinationLength ≤ characters.length ≤ 15
- characters consists of distinct lowercase English letters
- characters is sorted in ascending order
- At most 104 calls will be made to next and hasNext
-
It is guaranteed that all calls to
nextare valid
Visualization
Tap to expand
Understanding the Visualization
1
Initialize State
Set up indices array [0,1] for first combination "ab"
2
Generate Next
Find rightmost incrementable position and update to [0,2] for "ac"
3
Continue Pattern
Apply same logic to get [1,2] for "bc"
4
Detect End
When no position can be incremented, hasNext() returns false
Key Takeaway
🎯 Key Insight: Use indices array to represent combinations and apply next-permutation logic for O(k) space complexity with true lazy evaluation.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code