Monday 17 October 2016

Making Anagrams (CTCI)

Problem: Making Anagrams
Given two strings, determine the minimum number of character deletions required to make  and  anagrams. Any characters can be deleted from either of the strings.

int number_needed(string a, string b) {
   int countA[26], countB[26];
   for(int i=0;i<26;i++)
       countA[i]=countB[i]=0;
    
   for(int i=0;i<a.length();i++) 
       countA[ a[i]-'a']++;
   for(int i=0;i<b.length();i++) 
       countB[ b[i]-'a']++;
    
   int difference=0;
   for(int i=0;i<26;i++) 
       difference += abs(countA[i]-countB[i]);
   return difference;
}

No comments:

Post a Comment