Quickly count how many words a letter shows up in
I have an array of words, I need to figure out how many words each letter shows up in. The number of times per word doesn't matter, only the number of words.
I only need to check on a-z
, but since the array of words can be quite large (over 100,000) at a time, 26 iterations through the whole loop will take far too long for this.
What's a q开发者_JAVA百科uicker way to check this? 260,000 loops is far too many for this.
You have to loop through all the words, you could use count_chars
to give you all the unique letters used in each word quickly... but other than that there isn't much you can do. You can either test all letters against a word str_split
and array_unique
, or you can split the word into letters and find the unique ones count_chars
.
EDIT: If you are looking for absolute performance then you simply have to try all the different combinations. Point being, algorithm-wise, there isn't much one can do if your data is dynamic or "unknown".
精彩评论