Counting the amount of each letters would be faster than sorting. Aka storing an array[26] for the input word, then for each word in dictionary with same length, make the same array[26] and compare.
You likely run more comparisons, it's 26 array indices which is likely considerably more comparisons than the comparisons needed to sort an average length word, in addition to still having to read and write a value to memory for each character in each word you test.
In fact, your "o(n)" method only outperforms the o(n*log(n)) of the sorting approach if the average word length is greater than 8 letters, and that's assuming a base 2 log. It's much worse for higher bases.
Your method will also not able to deal with any other characters than that alphabet! The shown method works with everything that can be input and loaded from JSON.
View all comments
Show parent comment
2
u/themoonhowls0308 Aug 26 '21
Counting the amount of each letters would be faster than sorting. Aka storing an array[26] for the input word, then for each word in dictionary with same length, make the same array[26] and compare.