"Edgar" <mr********@yahoo.com> wrote in message <news:14*************************@posting.google.c om>...
This is more of a scrabble-game type of thing. Where you are given 7
letters and you are supposed to create a word or words with those...
This is actually not a hoomework assignment but I'm just curious to
see if there is something like this outthere. I am sure there is...
Are word-lists for games like Scrabble online anywhere? I'd start
with that instead of trying to generate all possible permutations
of seven letters. Instead, count the occurrences of each letter
in your 7-character input, then fetch words from your dictionary
that have equal or lesser counts of each letter.
Private Type Word
Word As String
Counts(vbKeyA To vbKeyZ) As Long
End Type
Public Sub Anagram(ByVal String1 As String)
Dim Words(1 To 4) As Word, Used() As Long
Words(1) = Anagram0("A")
Words(2) = Anagram0("ABA")
Words(3) = Anagram0("B")
Words(4) = Anagram0("BAB")
ReDim Used(1 To Len(String1))
Anagram1 Anagram0(String1), Words, Used, 0
End Sub
Private Function Anagram0(String1 As String) As Word
If Len(String1) = 0 Or String1 Like "*[!A-Za-z]*" Then Error 5
Dim i As Long, j As Integer
Anagram0.Word = UCase$(String1)
For i = 1 To Len(Anagram0.Word)
j = Asc(Mid$(Anagram0.Word, i, 1))
Anagram0.Counts(j) = Anagram0.Counts(j) + 1
Next
End Function
Private Sub Anagram1(Match As Word, Words() As Word, Used() As Long, ByVal Max As Long)
Dim i As Long, j As Integer, Temp As Word, Remain As Long, Start As Long
If Max = 0 Then Start = LBound(Words) Else Start = Used(Max)
For i = Start To UBound(Words)
Temp = Match
Remain = 0
For j = vbKeyA To vbKeyZ
If Words(i).Counts(j) = 0 Then
' skip it
ElseIf Words(i).Counts(j) > Temp.Counts(j) Then
Remain = -1
Exit For
Else
Temp.Counts(j) = Temp.Counts(j) - Words(i).Counts(j)
End If
Remain = Remain + Temp.Counts(j)
Next
If Remain = 0 Then
For j = 1 To Max
Debug.Print Words(Used(j)).Word; " ";
Next
Debug.Print Words(i).Word
ElseIf Remain > 0 Then
Used(Max + 1) = i
Anagram1 Temp, Words, Used, Max + 1
End If
Next
End Sub
In a "real" application, the words and their counts should be kept
in an indexed database table so you can query the table instead of
looping through the entire word list, and the output will have to
go somewhere other than just the Immediate window.
--
Joe Foster <mailto:jlfoster%40znet.com> "Regged" again? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!