Imports System
Imports System.Collections.Generic
Class Program
Private Shared Function IsInDict(ByVal word As String, ByVal dict As List(Of String)) As Boolean
For Each entry In dict
If entry = word Then Return True
Next
Return False
End Function
Private Shared Sub WordBreak(ByVal str As String, ByVal result As String, ByVal dict As List(Of String))
Dim strsize As Integer = str.Length
For i As Integer = 1 To strsize
Dim subStr As String = str.Substring(0, i)
If IsInDict(subStr, dict) Then
If i = strsize Then
Console.WriteLine(result & subStr)
Return
End If
WordBreak(str.Substring(i), result & subStr & " ", dict)
End If
Next
End Sub
Public Shared Sub Main()
Dim str As String = "butterflyplaybasketballwithbags"
Dim dict As List(Of String) = New List(Of String) From {
"butterfly",
"basketball",
"bagpiper",
"and",
"play",
"with",
"butter",
"fly",
"basket",
"ball",
"bags"
}
WordBreak(str, "", dict)
End Sub
End Class
' run:
'
' butter fly play basket ball with bags
' butter fly play basketball with bags
' butterfly play basket ball with bags
' butterfly play basketball with bags
'