[ C++ ] Using Bit manipulation | Detail Explain
This Question Look Like Little Complicated But It\'s Not actully In real \nSo Lets See One Simple Approch\n\nIdea :\n    step 1 . We Need To determind The all P
**This Question Look Like Little Complicated But It\'s Not actully In real \nSo Lets See One Simple Approch**\n\nIdea :\n    step 1 . We Need To determind The all Possiable lexicographical String With Having combinationLength \n            from That Given String\n    **Solution Of Step 1 :**\n       we are Going To Create A static function OR Global Function Which take that Input string and combinationLength \n       as an argument and return us A set of string having length of combinationLength in lexicographical form\nand we can genarete That all string by Using Bitmasking . How??Lets See.. fallowing\n        **Imp** Check string length and Then Itarate Through ```1 to that string length```\n            And consider Those Num which have ```set bit no ==  combinationLength```\n            then Just Take those character That lay on Set bit Possiation and Using of Those Create a String\n        Lastly Put each Of string into a String set and return that set\n\nLets take an Example To understand This Step\n\n\tSuppose Input string - "abc" and combinationLength =2\n        now mask = 8 which is nothing but 2 power length of of "abc"\n        now Itarate from 1 to 8 \n\n       Num             bit_rep           no_of_set_bit       a   b   c            hold_set\n        1               0 0 1                 1              0   0   1              nothing   <= because combinationLength != no_of_set_bit\n        2               0 1 0                 1              0   1   0              same case like upper one\n        3               0 1 1                 2              0   1   1              "bc" <= combinationLength ==no_of\n        4               1 0 0                 1              1   0   0              nothing\n        5               1 0 1                 2              1   0   1               "ac"\n        6               1 1 0                 2              1   1   0               "ab"\n        7               1 1 1                 3              1   1   1              nothing <= because combinationLength != no_of_set_bit\n\n        return that set in lexi order ["ab","ac","bc"]\n       \n    Now We Done Our Main Work\nstep 2. Take an iterator and initialize whith Set Begin\n        and for each ```next()```return it\'s Possiation string from set and increase that iterator\n        for ``` hasNext()``` Check iterator reach to then end of set or not and return that\n\n\n\n```\nset<string>GenaretallComb(string s,int len){\n    int mask = 1<<s.length();\n    set<string>hold;\n    string comString = "";\n    for(int no=1;no<mask;no++){\n        int num = no,i = 0;\n        while(num){\n            if(num&1)comString = comString + s[i];\n            i++,num>>=1;\n        }\n        if(comString.length()==len)hold.insert(comString);\n        comString = "";\n    }\n    return hold;\n}\n\nclass CombinationIterator {\npublic:\n    set<string>st;\n    set <string> :: iterator cur;\n    CombinationIterator(string characters, int combinationLength) {\n        this->st = GenaretallComb(characters,combinationLength);\n        this->cur = begin(st);\n    }\n    \n    string next() {\n        return cur==end(st)?"":*cur++;\n    }\n    \n    bool hasNext() {\n        return cur!=end(st);\n    }\n};\n```\n\nI hope It Help you **:)**\nPlease **upVote** Then\nAny Doubt comments Bellow\nThank You **:)**\n**Happy Coding**
[]