I have the following code to generate combinations of string for a small list and would like to adapt this for a large list of over 300 string words.Can anyone suggest how to alter this code or to use a different method?
Basically what i need to do is generate a list of string words in different combinations. The order does not change based on the list but for example if I had a list of integers: 1,2,3,4,5,6,7,8,9
then the combinations should be
12,23,34,45,56,67,78,89
123,234,345,456,567,678,789
1234,2345,3456,4567,5678,6789
etc all up to the 9
My code works for the list of 5 but if I had a list of 300 strings which I input from a file into an arraylist it takes too long.
here is my initial code with the small list
Imports System
Imports System.IO
Public Class combinations
Public Shared Sub main()
Dim myAnimals As String = "cat dog horse ape hen mouse"
Dim list As New ArrayList
Dim myAnimalCombinations As String() = BuildCombinations(myAnimals)
For Each combination As String In myAnimalCombinations
'Look on the Output Tab for the results!
Console.WriteLine("(" & combination & ")")
Next combination
console.ReadLine()
End Sub
Public Shared Function BuildCombinations(ByVal inputString As String) As String()
'Separate the sentence into useable words.
Dim wordsArray As String() = inputString.Split(" ".ToCharArray)
'A plase to store the results as we build them
Dim returnArray() As String = New String() {""}
'The 'combination level' that we're up to
Dim wordDistance As Integer = 1
'Go through all the combination levels...
For wordDistance = 1 To wordsArray.GetUpperBound(0)
Dim count = wordsArray.GetUpperBound(0)
'Go through all the words at this combination level...
For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance
'Get the first word of this combination level
Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex))
'And all all the remaining words a this combination level
For combinationIndex As Integer = 1 To wordDistance
combination.Append(wordsArray(wordIndex + combinationIndex))
Next combinationIndex
'Add this combination to the results
returnArray(returnArray.GetUpperBound(0)) = combination.ToString
'Add a new row to the results, ready for the next combination
ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)
Next wordIndex
Next wordDistance
'Get rid of the last, blank row.
ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1)
'Return combinations to the calling method.
Return returnArray
End Function
End Class
and the alterations to include arraylist
Private Function comboGenerator(ByRef chunk As ArrayList) As ArrayList
Dim phrase As String = Nothing
Dim size As Integer = chunk.Count
Dim myCombinations As String()
'BuildCombinations(myAnimals)
For i As Integer = 0 To size - 1
phrase = phrase & chunk.Item(i)
Next
myCombinations = BuildCombinations(phrase)
For j As Integer = 0 To chunk.Count - 1
chunk.Add(myCombinations(j))
Next
Return chunk
End Function
I kept the BuildCombinations function the same
Please advise.