Simple C++ Solution by Counting Letters, O(n + m), 3 ms

  • 0
    class Solution {
        char findTheDifference(string s, string t) {
            vector<int> cnt(128);
            for (char c: s) ++cnt[c];
            for (char c: t) if (cnt[c] <= 0) return c; else --cnt[c];
            return 0;

Log in to reply

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