Conversation
07d2709 to
d666833
Compare
|
Now I can describe how the cache works. First, let imagine that we allow duplicated anagrams like And in fact it is quite often that If we want to have maximum And if we don't want to generated duplicated anagrams, then additionally we need to have word |
|
I can suggest following implementation for your scenario. User provides input letters. We compute a tree with all results and show all (or top N) possible first words. So we just need to compute a tree, then everything can be taken from a tree with a constant time. In this case we don't need a depth parameter, so I have removed it in my third commit to make code a bit faster and even simpler. But of course we can still keep it if needed. |
|
Now we can compute a tree for |
|
In suggested solution we don't reuse the cache during a session, we just use it to compute a tree faster and then we use only the tree. |
|
In the "session" scenario we don't generate all anagrams and some code like |
|
I have reverted my changes and copied new implementation for session scenario into another package. |
|
I have implemented simple console application that allows to explore possible anagrams. |
I have started to think how can we optimize for the scenario that you were talking: start a session and explore possible anagrams.
As I understand, we don't need to show all (potentially millions) possible anagrams, so we don't need to generate them right away.
In my first commit I have changed that we don't generate all anagrams. I return only one of them for now, but we need to think what output do we want to show to the user? Maybe all possible words from which anagrams can be built? Or maybe top 10 that are commonly used? (we may need to use some external resource to sort words by how often they are used). Important part is that I still have a tree and all anagrams can be generated from it.
It takes 3100ms to generate all anagrams for
REFACTORING TO KOTL, but it takes only 1000ms to generate a resulting tree.In my second commit I have removed a code that makes sure that duplicated results like
A CATandCAT Aare not generated. Basically I have removed analogue of this:It is not an issue to have such duplicates in a resulting tree as we don't generate all results anyway. This makes code simpler and improves performance of resulting tree generation from 1000 to 700 for
REFACTORING TO KOTLinput.