4ms C++ solution


  • 0
    W

    Space is worse than bit manipulation but really not that bad.
    Speed is generally faster than bit manipulation since on the second path we don't have to traverse the whole thing, just to the point where we find the difference. Am I correct?

    class Solution {
    public:
        char findTheDifference(string s, string t) {
            // Hash table for known size
            vector<int> map(26, 0);
            // Hash
            for(char c: s)  map[c - 'a']++;
            // Search and update
            for(char c: t) {
                if(--map[c-'a'] < 0)
                    return c;
            }
            // Really should never reach here
            return '\0';
        }
    };
    

Log in to reply
 

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