Thanks for the inputs. I have tested different approaches with B4J. Bearing in mind that the GeneratePermutations Sub is recursive, that bit actually runs very fast.
[CODE lang="b4x" title="Generate Permutations"]
Sub GeneratePermutations(s As String) As List
Dim Possibilities As List
Possibilities.Initialize
If s.Length = 1 Then
Possibilities.Add(s)
Return Possibilities
End If
For i = 0 To s.Length - 1
Dim ch As String = s.CharAt(i)
Dim rem As String = s.SubString2(0, i) & s.SubString(i + 1)
Dim perms As List = GeneratePermutations(rem)
For Each perm As String In perms
Possibilities.Add(ch & perm)
Next
Next
Return Possibilities
End Sub[/CODE]
The problem arises when you test each possibility against an SQL dictionary for being a valid word. If it is valid the it is added to a list of the final result(s). Testing each one quickly becomes very processor intensive. For a 5 character word there are 5! permutations = 120 words, a 9 character word has 362,880 possible permutations. Using SQL the simple way is slow.....
[CODE lang="b4x" title="Test words for dictionary presence"]Sub Button1_Click
Dim Input As String = Dialog.TextInputDialog("Anagrams","Enter Letter Jumble","","")
If Input.Length = 0 Then Return
Dim Permutations, Results As List
Permutations.Initialize
Results.Initialize
Permutations = GeneratePermutations(Input)
MainForm.Title = Input & " " & Permutations.Size
For Each Word As String In Permutations
Dim found As String
found = SQL.ExecQuerySingleResult2("SELECT Word FROM English WHERE Word = ? " ,Array As String(Word))
If found <> Null Then
Results.Add(found)
End If
Next
End Sub
[/CODE]
Normally one would consider Transaction to speed up queries but here we are only reading data, not writing it. Even with a word of 8 characters my 20 core PC takes 100 seconds or so to produce the results in release, I don't see a handheld device doing it any quicker. The alternatives might be:
1. Creating a coded list in memory of dictionary words of matching input length. That may be the faster solution, but big memory overhead.
2. Alternatively creating a temporary SQL Table or View in memory (does B4X support that?) of matching input length and operating on that. I'm not sure that querying a table of 30,000 words is much faster than querying a table of 5000 ???
Any other suggestions?