C++ easiest solution


  • 0
    O
    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int sum = 0;
            for (char c : s) {
                sum -= c;
            }
            for (char c : t) {
                sum += c;
            }
            return char(sum);
        }
    };
    

    Better one:

    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int sum = 0;
            for (char c : s) {
                sum ^= c;
            }
            for (char c : t) {
                sum ^= c;
            }
            return char(sum);
        }
    };
    

Log in to reply
 

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