views:

61

answers:

2

i would like to take any input string and delimit it into groups. each group may be at least one character and no more than 4 characters. How do I loop to create all possible combinations?

Example:

in-string: xoox

output: x|o|o|x, x|oo|x, x|oox, xo|o|x, xo|ox, xoo|x, xoox

I'm writing an asp .net app using VB, but I really just need the concept for the recursion. So, an example in any language that doesn't use a special method to accomplish this would help me.

A: 

Interestingly, your visual representation using | characters to represent combined positions in the array gives a clue to how to approach this problem.

You have 2^(n-1) possible combinations where n is the length of the text. You can represent all the possible split positions using a list of binary numbers of length n:

000
001
010
011
100
101
110
111

You should be able to loop through this list of binary integers and, treating 0s for a position as false and 1s for a position as true, determine which combinations should be joined together. After you finish this loop, you'll have all possible combinations of the text.

Granted, there's still a good bit of VB.NET work to be done in order to generate the list of binary numbers, create output lists, etc, but hopefully this can get you started.

Ben McCormack
This is _almost_ right. You have to make sure that the binary numbers do not group more than 4 numbers together. For example, when you put in a string of length 5, you would get 1111 at the bottom of your binary list which is against the rules as the group's length is greater then 4.
huntaub
A: 

Something like this...

Sub writeDelimination(ByVal toSplit As String, ByVal start As String)
    Dim top As Integer = 4
    If toSplit.Length < 4 Then
        top = toSplit.Length
    End If
    For i As Integer = 1 To top Step 1
        Dim split As String = toSplit.Substring(0, i)
        writeDelimination(toSplit.Substring(i, toSplit.Length - i), start + "|" + split)
    Next
    If top = 0 Then
        Console.WriteLine(start)
    End If
End Sub

You would do the first call to the sub as writeDelimination("xoox", Nothing)

huntaub