Search code examples
javascriptalgorithmmathanagram

Math in this anagram substring search algorithm


In the following code:

function sherlockAndAnagrams(s) {
var pairs = 0;
var subStrings = {};

//find all substrings of our string, count them in a hash
for(var i = 0; i < s.length; i++){
    for(var j = i; j < s.length; j++){
        let tempSubString = s.substring(i, j+1).split("").sort().join("");
        if(subStrings[tempSubString]){
            subStrings[tempSubString] +=1;
        }else{
            subStrings[tempSubString] = 1;
        }
    }
}

//****ATTENTION******
for(var keys in subStrings){
    if(subStrings[keys] > 1){
    let temp = (subStrings[keys])*(subStrings[keys]-1)/2;
       pairs += temp;
   }
}
return pairs;
}

I am unsure how the math behind this formula works:

subString[keys])*(subString[keys]-1)/2

Assuming s = "kkkk", there would be 6 anagrams of "k"s

Can someone please explain this?

In addition, this kind of tells me that I may be a bit short on certain type of math. If you could advise me a good material to study for figuring out math like this I'd appreciate it!


Solution

  • The logic is following: you have n words that are anagrams of each other. How many pairs of anagrams can you select from there? This is a simple combinatoric question with a known answer: binominal coefficient of (n,2) which is n*(n-1)/2.

    The logic is that if you count all anagrams, the first anagram you can select in n ways, the second in n-1 ways but each anagram will be considered twice: once as ab=ba and another time as ba=ab