C++ Solution using hash


  • 0
    Y

    vector<int> dict is the hash table.

    class Solution {
    public:
        char findTheDifference(string s, string t) {
            char ans = 0;
            if(t.size() <= s.size())
                return ans;
            vector<int> dict(26, 0);
            for(int i = 0; i < s.size(); ++ i){
                ++ dict[s[i] - 'a'];
            }
            for(int i = 0; i < t.size(); ++ i){
                -- dict[t[i] - 'a'];
                if(dict[t[i] - 'a'] < 0){
                    ans = t[i];
                    break;
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

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