Easy to understand solution (C++) (O(1) space and O(n) time)


  • 0
    J
    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int sum = 0;
            int size = s.size();
            for (int i = 0; i < size; i++) {
                sum -= (int) s[i];
                sum += (int) t[i];
            }
            sum += (int) t[size];
            char result = static_cast<char>(sum);
            return result;
        }
    };
    

    After subtracting the converted int values of s from the values of t, the remaining number should be the value corresponding the the answer.


Log in to reply
 

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