Recursive Backtracking Solution with Explanation | C++
\n# Explanation\n Describe your approach to solving the problem. \nWe will be needing a set of string to store our combinations. We\'ll also need an iterator fo
\n# Explanation\n<!-- Describe your approach to solving the problem. -->\nWe will be needing a set of string to store our combinations. We\'ll also need an iterator for the next() and hasNext().\n + We create all the possible combinations of the given length in the comb() and insert them into the set. This ensures no duplicates are being considered and the lexicographical order as well.\n + We also initialize the iterator to s.begin() in the constructor.\n + next() : just return the iterator value as it is given that all next() calls will be valid. Also increment the iterator here.\n + hasNext() : if the iterator has reached the end of set return false, else true. \n\n# Code\n```\nclass CombinationIterator {\npublic:\n    set<string> s;\n    set<string>::iterator i;\n    void comb(int ind, string &str, string combo, int k){\n        if(k == 0){\n            s.insert(combo);\n            return;\n        }\n        if(ind == str.size()) return;\n\n        comb(ind+1,str,combo+str[ind],k-1);\n        comb(ind+1,str,combo,k);\n    }\n    CombinationIterator(string characters, int combinationLength) {\n        comb(0,characters,"",combinationLength);\n        i = s.begin();\n    }\n    \n    string next() {\n        return *(i++);\n    }\n    \n    bool hasNext() {\n        if(i == s.end()) return false;\n        return true;\n    }\n};\n\n/**\n * Your CombinationIterator object will be instantiated and called as such:\n * CombinationIterator* obj = new CombinationIterator(characters, combinationLength);\n * string param_1 = obj->next();\n * bool param_2 = obj->hasNext();\n */\n```
['C++']