Not the simplest but helpful ideas to solve harder problems C++ solution

  • 0

    Was working on a Anagram problem before this one and all I had to do is change on line for this to work. Basically you use a vector and ASCII values to determine how many times they appear in each word. One for loop adds to the vector at the position of the give letter and then the other for loop subtracts when it encounter that same letter.

       char difference(string s, string t)
        vector<int> freq (26,0);
        char answer = '\0';
        for(auto ss : s){ ++freq[ss - 'a']; }
        for(auto tt : t){ --freq[tt - 'a']; }
        for (int i=0; i<freq.size(); i++)
            if (freq[i] < 0) {
                answer = i + 'a';
        return answer;

Log in to reply

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