You are viewing a single comment's thread. Return to all comments →
JS
function sherlockAndAnagrams(string) { const pairs = new Map(); for (let size = 1; size <= string.length - 1; size++) { for (let index = 0; index <= string.length - size; index++) { const pairKey = string.slice(index, index + size).split("").sort().join(""); pairs.set(pairKey, (pairs.get(pairKey) || 0) + 1); } } return [...pairs.values()].filter(count => count > 1).reduce((pairs, count) => pairs + count * (count - 1) / 2, 0); }
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 →
JS