Sherlock and Anagrams in Javascript
comparing each with each means the quadratic time complexity, and skipping the ones with different length does not improve the bottomline.

Sort the set of (sorted) substrings, and the anagrams will form contiguous runs. The run of length \$k\$ produces \$\dfrac{k(k-1)}{2}\$ pairs.



Example:

An original array:

ab
cde
xy
dec
ba
ced


After each string has been sorted, it becomes

ab
cde
xy
cde
ab
cde


Now, after the entire array is sorted lexicographically, it becomes

ab
ab
cde
cde
cde
xy


The ab, ab and cde, cde, cde form the contiguous (that is, uninterrupted) runs, of length 2 and 3 respectively.