How to print all possible ways to break a string in brackets with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Sub break_string_in_bracket(ByVal str As String, ByVal index As Integer, ByVal form As String)
        If index = str.Length Then
            Console.WriteLine(form)
        End If

        For i As Integer = index To str.Length - 1
            Dim temp As String = form
            temp += "("
            temp += str.Substring(index, (i + 1) - index)
            temp += ")"
            break_string_in_bracket(str, i + 1, temp)
        Next
    End Sub

    Public Shared Sub Main()
        Dim str As String = "abcd"
	
        break_string_in_bracket(str, 0, "")
    End Sub
End Class




' run
'
' (a)(b)(c)(d)
' (a)(b)(cd)
' (a)(bc)(d)
' (a)(bcd)
' (ab)(c)(d)
' (ab)(cd)
' (abc)(d)
' (abcd)
' 

 



answered Aug 27, 2023 by avibootz
...