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.
publicstaticintanagram(Strings){if(s.length()%2!=0)return-1;// If length is odd, return -1int[]frequency=newint[26];// Array for character frequenciesintmid=s.length()/2;for(inti=0;i<mid;i++){frequency[s.charAt(i)-'a']++;// Count chars in first halffrequency[s.charAt(i+mid)-'a']--;// Subtract chars in second half}intchanges=0;for(intcount:frequency){if(count>0)changes+=count;// Count excess chars that need replacement}returnchanges;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Anagram
You are viewing a single comment's thread. Return to all comments →
My Java solution: