Re: find words that contains some specific letters
In article
<d121e6f6-6afa-43e7-bcd1-c9a770e12704@z9g2000yqi.googlegroups.com>,
Fralentino <baretos@gmail.com> wrote:
On 1 Juni, 09:20, Andrew Thompson <andrewtho...@gmail.com> wrote:
On Jun 1, 4:16?pm, Fralentino <bare...@gmail.com> wrote:
...I have a solution for this ..
What is your solution?
My solution:
boolean compareLetters(String toBeCompared,ArrayList<String> letters)
{
for(int i = 0;i<toBeCompared.length();i++)
{
if(letters.contains(""+toBeCompared.charAt(i)))
{
letters.remove(""+toBeCompared.charAt(i));
}
else
return false;
}
return true;
}
IIUC, you then have to run this method on every word in your dictionary.
Another approach is to permute the letters and search the dictionary.
A binary search of an ordered word list is O(log n), worst case.
Here's an example in Ada:
<http://home.roadrunner.com/~jbmatthews/jumble.html>
In Java, permutations can be checked quickly with the contains() method
of the Set interface, once the dictionary is read:
private static final String NAME = "/usr/share/dict/words";
private static final Set<String> set = new TreeSet<String>();
static {
try {
File file = new File(NAME);
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
String s;
while ((s = in.readLine()) != null) {
set.add(s);
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
Finding the permutations of a string in Java is straightforward:
<http://www.google.com/search?q=java+permute>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>