You are viewing a single comment's thread. Return to all comments →
js
function sherlockAndAnagrams(s) { const map = {}; let count = 0; for (let i = 0; i < s.length; i++) { for (let j = i + 1; j <= s.length; j++) { const subword = s.substring(i, j); const sortedSubWord = [...subword].sort().join(""); map[sortedSubWord] = (map[sortedSubWord] ?? 0) + 1; } } for (let [key, v] of Object.entries(map)) { count += (v * (v - 1)) / 2; } 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 →
js