Anagram finder

BlitzMax Forums/BlitzMax Beginners Area/Anagram finder

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hi,
I think that your question is quite difficult to answer to.

Due of the nature of anagrams, you may find out that with a simple word of 9 letters you can generate lots of letter and word combinations.

What I suggest you, is to solve this problem from another point of view.

An hint I give you, is a simple way to check if the user has typed an anagram of a word; the simplest way is to use a dictionary made of "base anagrams".

I try to explain this concept with an example of what I mean.

Let's take the word: CAR.

An anagram of the word CAR is: ARC.

How we check that ARC is an anagram of CAR ?

Well, let's build the base anagram of the word CAR, which is made in this way: we build a string with all the characters composing the word, and sort them alphabetically:

So:
CAR is composed by C,A, and R, which sorted generates: ACR

and in the same way:
ARC is composed by A,R, and C, which sorted generates: ACR

As you see, ARC and CAR generate the same base anagram: ACR.

In this way we can check if a word is an anagram of another word, by simply checking if the base anagram of a word matches the other ones.

Now, if you have already a word dictionary, would be easy to build up a base anagram dictionary too.

That way, when the player type a sentence, you may simply:
- build a base anagram for each word, and check if it exists in your base anagram dictionary (or check the word in the normal dictionary of course) and then
- build a string with all the base anagrams sorted. Now, if this string has the same base anagram of the initial given word, that means that the player has made a valid anagram.

Example with ALUMINIUM. Let's say that the player writes the sentence:
MAUL IN MUI
and let's think that this sentence is a valid english syntax.

So, the 3 words together generate a base anagram of:
( ALMU + IN + IMU = ALMUINIMU) which sorted gives:
AIILMMNUU

Now, if you take the word ALUMINIUM and build up its base anagram, you will find also:
AIILMMNUU

The two base anagram matches ! That means, the player has typed a valid anagram :)

Hope it helps,
Sergio.