We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Solution using int array for 26 English letters:
publicstaticintmakeAnagram(Stringa,Stringb){// Since there are only 26 lowercase English lettersint[]charCount=newint[26];intdeleteCount=0;// Count frequency of each character in string 'a'for(charc:a.toCharArray()){charCount[c-'a']++;}// Adjust frequency based on characters in string 'b'for(charc:b.toCharArray()){charCount[c-'a']--;}// Sum up the absolute differences to calculate deletionsfor(intcount:charCount){deleteCount+=Math.abs(count);}returndeleteCount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
Java Solution using int array for 26 English letters: