程式語言 - LeetCode - C++ - 389. Find the Difference



題目:


解答:

class Solution {
public:
    char findTheDifference(string s, string t) {
        vector<int> cnt(26, 0);

        for (char c : s) {
            cnt[c - 'a'] += 1;
        }

        for (char c : t) {
            if (cnt[c - 'a'] == 0) {
                return c;
            }

            cnt[c - 'a'] -= 1;
        }

        return 0;
    }
};