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.
Solution in JS,
Create a Map with all the consecutives substrings, and sort each one to simplify the search in the map, and use the acumulation to simplify the last sumatory,
functionsherlockAndAnagrams(s){// Write your code hereletout=0;lethash={}for(leti=0;i<s.length;i++){for(letj=i+1;j<s.length+1;j++){letc=[...s.substring(i,j)].sort().join('');console.log(c,hash[c])out+=hash[c]||0if(cinhash)hash[c]++elsehash[c]=1;}}returnout}
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 →
Solution in JS, Create a Map with all the consecutives substrings, and sort each one to simplify the search in the map, and use the acumulation to simplify the last sumatory,