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.
Here is my solution using a hash function to count number of pais:
functionsherlockAndAnagrams(s){// Write your code hereletcount=0;for(leti=1;i<s.length;i++)count+=subs(s,i);returncount;functionsubs(s,size){leti=0;lethashs={};while(i+size<=s.length){letsub=s.substring(i,i+size);lethash=makeHash(sub);if(!hashs[hash])hashs[hash]=0;hashs[hash]+=1;i++;}letsum=0;letkeys=Object.keys(hashs)for(leti=0;i<keys.length;i++)if(hashs[keys[i]]>1)sum+=hashs[keys[i]]*(hashs[keys[i]]-1)/2;returnsum;}functionmakeHash(s){leta=[];for(leti=0;i<s.length;i++)a.push(s.charAt(i));a.sort();returna.toString();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
Here is my solution using a hash function to count number of pais: