3 lines C++ solution using bit manipulation - 4ms


  • 1
    B
    char findTheDifference(string s, string t) {
        for(int i=1;i<s.size();i++)
            s[i]^=(s[i-1]^t[i-1]);
        return s[s.size()-1]^t[t.size()-1]^t[t.size()-2];
    }
    
    Identical letters will be 0 after xor manipulation, the remain one is the letter added.

Log in to reply
 

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