All ways to partition a string

后端 未结 5 1964
深忆病人
深忆病人 2021-01-01 00:08

I\'m trying to find a efficient algorithm to get all ways to partition a string

eg for a given string \'abcd\' =>
\'a\' \'bcd\'
\'a\' \'b\' \'cd\'
\'a\'

5条回答
  •  迷失自我
    2021-01-01 00:34

    Something along the lines of the following (untested and likely buggy VB.NET sample)

    Function FindAllGroups(s As String) As List(Of List(Of String))
        Dim ret As New List(Of List(Of String))
        Dim l As New List(Of String)
        l.Add(s) 'the whole string unbroken
        ret.Add(l) 'first option we return is the whole unbroken string by itself
        If s.Length > 1 Then
            Dim tmp = FindAllGroups(s.Substring(1)) 'find all the groups for the rest of the string after the first character
            For Each l2 in tmp
                l = l2.ToList 'Copy it
                l.Insert(s.SubString(0,1),0)'insert the first character from this string by itself before this combination for the rest of the string
                ret.Add(l)
            Next
            For Each l2 in tmp
                l = l2.ToList 'Copy it
                l(0)= s.SubString(0,1) & l(0) 'insert the first character from this string as part of the first element in the list
                ret.Add(l)
            Next
       End If
       Return ret
    End Function
    

    This basically works by saying that we can take 'abcd' and split it into

    'a', 1st option for 'bcd' split
    'a', 2nd option for 'bcd' split
    ...
    +
    1st option for 'bcd' split with the first element prepended with 'a'
    2nd option for 'bcd' split with the first element prepended with 'a'
    ...
    

    then to calculate 'bcd', we just repeat the process as above, only with

    'b', 1st option for 'cd' split
    'b', 2nd option for 'cd' split
    ...
    +
    1st option for 'cd' split with the first element prepended with 'b'
    2nd option for 'cd' split with the first element prepended with 'b'
    ...
    

    etc. repeated recursively.

    However, this code isn't particularly efficient at runtime. One thing that you could do to speed it up significantly would be to add a Dictionary(Of String, List(Of List(Of String)) outside the function which you can store a cache of the results in and if the item exists in there, you return from there, if not, calculate it and add it. Lists also might not be the most efficient, and the ToList function might not be the quickest way of cloning. However, I've simplified it to make it easier to understand and also to save me time working it out!

提交回复
热议问题