C++ Solution using XOR


  • 0
    Y

    Here is the code:

    class Solution {
    public:
        char findTheDifference(string s, string t) {
            char ans = 0;
            if(t.size() <= s.size())
                return ans;
            for(int i = 0; i < s.size(); ++ i){
                ans ^= s[i];
            }
            for(int i = 0; i < t.size(); ++ i){
                ans ^= t[i];
            }
            return ans;
        }
    };
    

Log in to reply
 

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