Iterator for Combination - Problem
Design a Custom Iterator for String Combinations

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 exist

For 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 next are valid

Visualization

Tap to expand
Combination Iterator State Machine[0,1]"ab"Initial State[0,2]"ac"Next State[1,2]"bc"Final Statenext()next()State Transition AlgorithmInput: characters = "abc", k = 2State: indices = [i, j] where 0 ≤ i < j < nTransition: Find rightmost index that can be incrementedTermination: When indices = [n-k, n-k+1, ..., n-1]Example: [0,1] → [0,2] → [1,2] → END
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.
Asked in
Google 45 Microsoft 38 Amazon 32 Apple 28
42.3K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen