• + 1 comment

    That's the most efficient way to solve it using bit manipulation because low-level techniques are usually fast. If you really care about speed then consider using int as the data holder and use bitwise operation to perform things. But if you want to write code that are easier to read that mimic bitwise then use bitset because there are built-in functions and overloaded operators for it.

    bitset<26> gem, elems;

    gem.set();

    for each c in string {elems.set(c-'a')}

    gem &= elems;