Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word puzzle / unscramble 1

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
I need to create a fun/educational project for my children that will unscramble entered string to possible combinations of words with minimum 2 char and maximum of the length entered string.
If I enter a string like "NEAT" it should return all combinations of letters entered (that not necessarily meaningful) and populate a listbox with

AE
AN
AT
EA
EN
ET
ATE
EAT
NAT
TEA
....
.....

any one got an idea...
thanks


________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
You also to need to consider that the order of the letters matters. In your example above, you listed

AE
AN
AT
EA
EN
ET

as 2-letter combinations. However, you need to do the reverse of all of those as well, because letter order can make the same 2 letters into 2 different words, such as 'on' and 'no'. This becomes even more difficult as you progress into 3 letters, 4 letters, etc.

In mathematical terms, what you are doing is finding the permutations in a given set of letters. The basic forumla for calculating the number of permutations (p) of n groups from a population of m letters:

p = m!/(m - n)!

In the case of your example above, the number of 2-letter combinations is:

p = 4!/(4 - 2)! = 4!/2! = (4*3*2*1)/(2*1) = 24/2 = [green]12[/green]

Note that this is all combinations, even 2 of the same letter.

Now you have to do the same for permutations of 3 leters, and of 4 letters.

4 letters = 4!/(4-4)! = 24/0! = 24/1 = [green]24[/green]

3 letters = 4!/(4-3)! = 24/1 = [green]24[/green]

2 letters = 4!/(4-2)! = 24/2 = [green]12[/green]

1 letter = 4!/(4-1)! = 24/6 = [green]4[/green] (don't forget the 1 letter words!)

Note that in the 4-letter formula that 0! = 1. This is correct, but I'm not going to go into why here.

So, given the above, there are 64 possible combinations of 4, 3, 2 or 1 letters from a 4 letter word.

I don't know if this will help you, but it can at least give you a way to determine the number of combinations you need to process for each set of letters entered.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
How about something like this:

1) Declare a global ArrayList
Code:
[COLOR=blue]Dim[/color] myArray [COLOR=blue]As[/color] [COLOR=blue]New[/color] ArrayList

2) The function
Code:
    [COLOR=blue]Private[/color] [COLOR=blue]Sub[/color] GetPermutationArrayList([COLOR=blue]ByVal[/color] PrefixString [COLOR=blue]As[/color] [COLOR=blue]String[/color], [COLOR=blue]ByVal[/color] Permutation [COLOR=blue]As[/color] [COLOR=blue]String[/color], [COLOR=blue]ByRef[/color] GlobalArrayList [COLOR=blue]As[/color] ArrayList, [COLOR=blue]Optional[/color] [COLOR=blue]ByVal[/color] minimumLength [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] = 1)
        [COLOR=blue]If[/color] PrefixString.Length >= minimumLength [COLOR=blue]Then[/color] myArray.Add(PrefixString)
        [COLOR=blue]For[/color] intIndex [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] = 1 [COLOR=blue]To[/color] Permutation.Length
            GetPermutationArrayList(PrefixString & Permutation.Substring(intIndex - 1, 1), Permutation.Substring(0, intIndex - 1) & Permutation.Substring(intIndex), GlobalArrayList, minimumLength)
        [COLOR=blue]Next[/color]
    [COLOR=blue]End[/color] [COLOR=blue]Sub[/color]

3) Call the function to populate the array and tell it the minumum number of characters to have
Code:
GetPermutationArrayList("", "NEAT", myArray, 2)





____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Oops.

In my post, I stated Note that this is all combinations, even 2 of the same letter.

This is actually not correct. It will count 2 of the same letter if that letter is repeated in the word (like 't' in attend).

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
I should say WOW WOW WOW..ca8msm.
That was fantastic...

Found a bug too.. If a letter repeated in the input string then it creates combinations twice or more as the length of word increases or the repeatation increases.

jbenson, thanks for your input too..

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Found a bug too.. If a letter repeated in the input string then it creates combinations twice or more as the length of word increases or the repeatation increases.
Yeah, I spotted that when writing it but I left it in to show all the different combinations of the word. i.e. in the string AA there are two combinations:

1) the first letter and the second letter
2) the second letter and the first letter

In your case though, you are actually looking for words (even if the don't actually mean anything or are not a "real" word). So, if you don't want to include them, simply change:
Code:
[COLOR=blue]If[/color] PrefixString.Length >= minimumLength [COLOR=blue]Then[/color] myArray.Add(PrefixString)
to
Code:
[COLOR=blue]If[/color] PrefixString.Length >= minimumLength [COLOR=blue]AndAlso[/color] myArray.IndexOf(PrefixString) = -1 [COLOR=blue]Then[/color] myArray.Add(PrefixString)


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Wonderful..
Thanks ca8msm..

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top