Problem solution · C++

Iterator for Combination

Iterator for Combination: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
78 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Iterator for Combination, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 78 lines of C++ from the credited upstream file iterator-for-combination.cpp.
  • The implementation visibly relies on sequence storage.
  • 2 loop blocks detected.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeIterator for Combination · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(k), per operation// Space: O(k) class CombinationIterator {public:    CombinationIterator(string characters, int combinationLength)    : characters_(characters)    , combinationLength_(combinationLength)    , last_(prev(characters.cend(), combinationLength), characters.cend())    , stk_{bind(&CombinationIterator::divide, this, 0)} {             }        string next() {        iterative_backtracking();        return curr_;    }        bool hasNext() {        return curr_ != last_;    } private:    void iterative_backtracking() {        while (!stk_.empty()) {            const auto cb = move(stk_.back());            stk_.pop_back();            if (cb()) {                return;            }        }    }        bool conquer() {        if (curr_.length() == combinationLength_) {            return true;        }        return false;    }        bool prev_divide(char c) {        curr_.push_back(c);        return false;    }        bool divide(int i) {        if (curr_.length() != combinationLength_) {            for (int j = int(characters_.length()) - (combinationLength_ - int(curr_.length())  - 1) - 1;                 j >= i; --j) {                stk_.emplace_back(bind(&CombinationIterator::post_divide, this));                stk_.emplace_back(bind(&CombinationIterator::divide, this, j + 1));                stk_.emplace_back(bind(&CombinationIterator::prev_divide, this, characters_[j]));            }        }        stk_.emplace_back(bind(&CombinationIterator::conquer, this));        return false;    }        bool post_divide() {        curr_.pop_back();        return false;    }        const string characters_;    const int combinationLength_;    string curr_;    string last_;    vector<function<bool()>> stk_;}; /** * Your CombinationIterator object will be instantiated and called as such: * CombinationIterator* obj = new CombinationIterator(characters, combinationLength); * string param_1 = obj->next(); * bool param_2 = obj->hasNext(); */  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗