Hi,

I want to find all the words(scabble words) that can be made out of given set of letters.
ex: abnana

results: a aa ab an baa ban banana na nab

How could i achieve this? Do i need a binary tree?
Plz some one help me...

This seems more like a job for a frequency list. You can use a dictionary of characters and counts like so:

for each word in the dictionary
    build a frequency list of each character initialized to 0

    for each character in the letter collection
        if the character exists in the frequency list
            increment the count for that character

    if all counts in the frequency list are greater than 0
        mark the word as a match

I think the question is actually "how do I generate permutations?"

"nab" isn't a permutation of "abnana". Permutations can get you closer to a complete solution, but that's not all there is to it.

Im a student so Im not expert in java.But not even too much low.
I dont understand the frequency list.
If u could please explain the psuedo code a bit?

I dont understand the frequency list.

http://en.wikipedia.org/wiki/Letter_frequencies.

If u could please explain the psuedo code a bit?

You're basically just matching at least one of every letter in a given word using the available tiles that the player currently has. All of the words that meet this requirement are words that the player can make with his or her tiles.

Probably the easiest way is to put all the words in a TreeMap (for fast searching) then check every permutation of every subset of your letters to see if it's in the TreeSet. However that will generate a lot of searches if you have a lot of letters, which may or may not make it too slow.

You can speed up the search, at the cost of more work at startup time, by building a TreeMap. For each word sort its letters into order and use that as the key; the word (or words)corresponding to that form the value. Now you just need to test each subsets of your letters once - keep them sorted and see if the subset is a valid key. There's no need to test all the permutations.

Decepticon's solution may be faster or easier than either of these, but I'm ashamed to admit that I don't understand it.

Decepticon's solution may be faster or easier than either of these, but I'm ashamed to admit that I don't understand it.

Here's a quickie example to show what I mean. Please excuse the crappy Java, I'm terribly rusty:

import java.util.*;

class Foo {
    public static void main(String args[])
    {
        String[] dictionary = {
            "za",  "zoo", "a",      "aa", "ab",  "an", 
            "baa", "ban", "banana", "na", "nab", "test"
        };
        String word = "abnana";

        for (int i = 0; i < dictionary.length; i++) {
            TreeMap<Character, Integer> freq = new TreeMap<Character, Integer>();

            for (int j = 0; j < dictionary[i].length(); j++)
                freq.put(dictionary[i].charAt(j), 0);

            for (int j = 0; j < word.length(); j++) {
                if (freq.containsKey(word.charAt(j)))
                    freq.put(word.charAt(j), freq.get(word.charAt(j)) + 1);
            }

            boolean match = true;

            for (int count : freq.values()) {
                if (count <= 0) {
                    match = false;
                    break;
                }
            }

            if (match)
                System.out.println("'" + dictionary[i] + "' was found");
        }
    }
}

OK, thank you. That seems functionally equivalent to creating a key that contains the letters in the word but sorted - ie its a way of identifying words that is independent of the order in which its letters appear. (Correct me if I'm wrong, please.)

ie its a way of identifying words that is independent of the order in which its letters appear.

Yes, pretty much.

"nab" isn't a permutation of "abnana". Permutations can get you closer to a complete solution, but that's not all there is to it.

True, you'd also need to look at subsets of the original set of letters.

For completeness, you'd also want to consider a nonempty game board, which would involve finding words with specific letters in fixed positions.

Edited 4 Years Ago by gusano79: flow

Here's a quickie example to show what I mean. Please excuse the crappy Java, I'm terribly rusty:

Thank you very much.
I get it now and you just saved me :-)

This question has already been answered. Start a new discussion instead.