I'm going with O(n log n + (n-1)) as fastest

I think the best algorithm is to sort first and then check for pairs.

Sorting O(n log n) and then comparing pairs just once O(n) is so much faster than checking all letters against all other letters O(n^2).

This is also clearly shown by the graph from WolframAlpha

Ohhh, and I failed…:wink:
Should have done some more thinking and googling.

Of course checking each letter only once without any sorting, O(n), is again very much faster.

Privacy & Terms