C++ Bit Solution, Just because I like having the excuse to use log2()


  • 1
    F

    This was more for fun than finding the fastest answer....

    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int bits1 = 0;
            int bits2 = 0;
            char aMinus1 = 'a'-1;
            
            for(auto ch : s)  bits1 ^= 1 << (ch-aMinus1);        
            for(auto ch : t)  bits2 ^= 1 << (ch-aMinus1);
           
            return aMinus1 + log2(bits1 ^ bits2); 
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.