Fralentino wrote:
> Hi!
>
> I want to make an algorithm that searches for all words in a
> dictionary that contains some specific set of letters. For example i
> want to find all words containing the exact letters t,a,s,m,p so the
> word stamp is ok.
>
> So basically i want to compare a String with a set of letters and find
> out if you can build this string with these exact letter. I have a
> solution for this but i dont think it's the the most optimal one so i
> would like som tips on how do to this in a optimal and rather easy
> way. Is there some recommended designs in how to do this? Is there
> perhaps some method or class in the java api that does this for you?
>
> Thanks.
> /Baretos
Assuming that we are talking about a fresh search every time, your
proposed algorithm (later post) is probably not all that bad, although
I'd be inclined to take the letters of interest, put them in a HashSet,
and then process each letter of a dictionary word and see if the set
contains it.
Any near-optimal method would also take into account the frequency of
letters in a language. For example, if you saw that your set of letters
contained one rare letter, for example, you'd save time overall by first
doing a search for this letter specifically in each dictionary word, and
discarding all of them (the large majority) that do not contain that
rare letter (*). Then you can do the comprehensive search.
AHS
* If your entire dictionary was composed into a single string, and you
maintained an offsets table, you could just burn through that large
string and look for the rare letter locations, and from the table figure
out what words contain it.
|