← Home

1286. Iterator for Combination

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
128

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1286. Iterator for Combination, the solution in this repository is mainly a data structure design solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are next, hasNext.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 20 ms, faster than 27.38% of C++ online submissions for Iterator for Combination.
02//Memory Usage: 13.8 MB, less than 100.00% of C++ online submissions for Iterator for Combination.
03
04class CombinationIterator {
05public:
06    CombinationIterator(string characters, int combinationLength) {
07        this->characters = characters;
08        this->combinationLength = combinationLength;
09        
10        for(int i = 0; i < combinationLength; i++){
11            comb.push_back(i);
12        }
13    }
14    
15    string next() {
16        if(!hasNext()){
17            return cur;
18        }
19        string tmp;
20        tmp.clear();
21        for(int i = 0; i < combinationLength; i++){
22            tmp += characters[comb[i]];
23        }
24        cur = tmp;
25        
26        //update comb
27        bool updated = false;
28        //start from tail
29        for(int i = combinationLength - 1; i >= 0; i--){
30            //if comb[i] is already the last possible character
31            if(comb[i] != characters.size() - 1 - (combinationLength - 1 - i)){
32                comb[i]++;
33                //move the later characters to the just left of current updated position
34                for(int j = i+1; j < combinationLength; j++){
35                    comb[j] = comb[j-1]+1;
36                }
37                updated = true;
38                break;
39            }
40        }
41        
42        return cur;
43    }
44    
45    bool hasNext() {
46        return  cur != characters.substr(characters.size() - combinationLength, combinationLength);
47    }
48    
49    vector<int> comb;
50    string characters;
51    int combinationLength;
52    string cur;
53};
54
55/**
56 * Your CombinationIterator object will be instantiated and called as such:
57 * CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
58 * string param_1 = obj->next();
59 * bool param_2 = obj->hasNext();
60 */

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.