IsIsogram() best order

I think the best order for IsIsogram() is O(n). Best case, there will only be two of the same letter. As soon as the duplicate is spotted, we know that the word is not an isogram. It doesn’t matter if there are three or more of the same letter, since as soon as a duplicate is spotted, the function will return that the word is not an isogram. The sooner a duplicate is seen by the function, the faster it will return the value. At worst, there are no duplicates, in which case, the function will return that the word is an isogram. This is the slowest case, but it is still faster than comparing every letter to each other, or even sorting first, since it eliminates the other steps present in the O(n^2) and O(n log n) algorithms.

1 Like

Privacy & Terms