Making Anagrams

  • + 0 comments

    C++

    int makingAnagrams(string s1, string s2) {
        vector<int> vec1(26, 0);
        vector<int> vec2(26, 0);
    
        int common = 0;
        for (int i = 0; i < s1.size(); i++)
            vec1[s1[i] - 'a']++;
        for (int i = 0; i < s2.size(); i++)
            vec2[s2[i] - 'a']++;
    
        for (int i = 0; i < 26; i++) {
            int freq1 = vec1[i];
            int freq2 = vec2[i];
    				
            if (freq1 != 0 && freq2 != 0) 
                common += min(freq1, freq2);
        }
        return s1.length() + s2.length() - 2 * common;
    }