You are viewing a single comment's thread. Return to all comments →
I think you are missing a step. You have to check is substr is not '' otherwise you will get a lot of occurrencies.
function sherlockAndAnagrams(s) { let count = 0; for (let i = 0; i < s.length; i++) { let found = {}; for (let j = 0; j + i <= s.length; j++) { let substr = s.substr(j, i); if (substr) { substr = substr.split('').sort().join(''); if (found[substr]) { count += found[substr]; found[substr]++; } else { found[substr] = 1; } } } } return count; }
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 →
I think you are missing a step. You have to check is substr is not '' otherwise you will get a lot of occurrencies.