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.
I also started from HashMap but than I realized that getting value for each containsKey=true case is a bit of complicated. I also did not like the extra loop which I had to use to validate the collected data. So I used int [] and processed the values through summ variable right inside the first loop:
staticbooleanisAnagram(Stringa,Stringb){if(a.length()!=b.length())returnfalse;a=a.toLowerCase();b=b.toLowerCase();int[]temp=newint[Character.MAX_VALUE];//sry, too lazy to calculate ASCII idx of a-z rel. i :)intsumm=0;for(inti=0;i<a.length();i++){summ+=++temp[a.charAt(i)]<=0?-1:1;summ+=--temp[b.charAt(i)]>=0?-1:1;}returnsumm==0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
I also started from HashMap but than I realized that getting value for each containsKey=true case is a bit of complicated. I also did not like the extra loop which I had to use to validate the collected data. So I used int [] and processed the values through summ variable right inside the first loop: