Simple C++ code using map


  • 0
    A

    We can keep track of number of times a characters is repeated in a string using a map.

    char findTheDifference(string s, string t) {
            map<char, int> counts;
            
            for(char ch : s){
                counts[ch] ++;
            }
            
            for(char ch : t){
                if(counts[ch] == 0) return ch;
                counts[ch]--;
            }
            
            return '0';
        }
    

Log in to reply
 

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