将字符串拆分为所有可能的子字符串,以便连接字符串创建原始字符串?

时间:2019-06-18 01:21:52

标签: excel vba

我想以所有可能的方式拆分字符串,以便在子字符串连接在一起时,它们等于原始字符串。例如,“ vba”的输出为[[vba],[v,b,a],[vb,a],[v,ba]]。这是与Find all the combination of substrings that add up to the given string相同的问题,但是我在VBA中实现时遇到了麻烦。 我的VBA代码遇到了一些麻烦,我无法弄清楚出了什么问题。有任何想法吗?这是我第一次使用VBA,并且我更习惯Java,所以我认为我可能不正确地重新分配了变量?

我尝试了几种方法,但这是我最近的尝试:


Function solve(original) as Collection
    Dim result As New Collection
    Dim resultList As New Collection

    result.add Right(original, Len(original))
    resultList.add result

    Dim i As Integer
    Dim j as New Collection

    For i = Len(original)-1 To 1 Step -1
        Dim nList as New Collection
        For Each j In resultList
            Dim split as Collection, add As Collection
            Set split = j
            split.add Mid(original, i, i+1), before:=1

            Dim first as String, second as String
            first=j(1)
            second = Mid(original, i, i+1) & first
            Set add = j
            add.Remove(1)
            If add.count = 0 Then
                add.add second
            Else
                add.add second, before:=1
            End If
            nList.add split
            nList.add add
        Next j
        Set resultList = nList
        Set nList = Nothing
    Next

    Set solve = resultList
End Function

0 个答案:

没有答案