Another easy solution, but slower


  • 0
    V
    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int s_len = s.length();
            int t_len = t.length();
            const int n = 26;
            vector<int> cnt(n, 0);
            
            for (int i = 0; i < s_len; i++)
            {
                cnt[s[i] - 'a']++;
            }
            
            for (int j = 0; j < t_len; j++)
            {
                cnt[t[j] - 'a']--;
            }
            
            for (int k = 0; k < n; k++)
            {
                if (cnt[k] < 0)
                    return k + 'a';
            }
            
            return 'a';
        }
    };
    

Log in to reply
 

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