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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding all letter combinations of a word.

Status
Not open for further replies.

wraygun

Programmer
Dec 9, 2001
272
US
I'm looking to take a word, anywhere from 3 to 10 letters, and determine all combinations without repeating the letters and assign them to an array. I've gotten far enough to assign the individual letters to an array, but need some direction on the rest. Any input will be appreciated.

Thanks,
WrayGun

Public Sub FindTheWords()
Dim Cnt As Integer
Dim Letters(100) As Integer
For Cnt = 1 To Len(Text1.Text)
Letters(Cnt) = Mid(Text1.Text, Cnt, 1)
Next
End Sub


***You can't change your past, but you can change your future***
 
My apologies, the sub should look like this:

Public Sub FindTheWords()
Dim Cnt As Integer
Dim Letters(100) As String
For Cnt = 1 To Len(Text1.Text)
Letters(Cnt) = Mid(Text1.Text, Cnt, 1)
Next
End Sub


***You can't change your past, but you can change your future***
 
Looks like a straightforward nested loop problem. In an 'n' letter word there are n ways of choosing the first letter, (n-1) ways of choosing the second etc.

In your starting code above you appear to be trying to assign string values to an Integer array - you'll need to change the Dim statement to suit!

If your array is only to take your original letters then it only needs to be
Dim Letters(9) as String

If it's intended for all letter combinations for a 10-letter word then it need to be a bit bigger - 10! is 3,628,800


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Not as straightforward as you might think, I found this looking for something else, but bookmarked it in case. It seems to take a string with a space separator between each letter or word and returns a single string whith cr/lf separators between each combination. Could form the basis for what you want ?

I just did a quick cut and paste, theres a typo in the code theres a ,_ should be space between them, but seems to work.

 
small point johnwm

but if any letters in the source string are repeats it does reduce the odds but point taken - it can't be guaranteed.

Surely if this is to be further processed like compared with entries in a database they have to be offered to the next process one by one or in managable chunks at least.
 
TThis is a 'classic' stats problem. (permutations and combinations). For [b[VERY SHORT[/b] 'strings", it is possible using a number of approaches, at least one of which is shown in many text books as an example of recursion. Unfortunatly, the number of combinations of N items is a (reapidly expanding) exponential function:

C(N) = N!/ (N! * (R - N)!

where
CN is n the number of combinations.
N is the number of items in the list, and
R is the number taken at each combination step

for six "letters", we are dealing with only 720 combinations. However proceeding upwards:
7 ~ 5040
8 ~ 40320
9 ~ 362880
10 ~ 3628800

So, it is reasonable to assume that a procedure based on recursion will have some difficulties before reaching the end of even this rather limited example.

Other approaches might also encounter various difficulties. Typically, the approaches are (or at least include) writting the various elements to somewhere as they are calculated.

This is generally far to slow to please the casual user.





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top