Print all the possible combinations of “X” amount of characters with “X” string length (Brute Force)

ⅰ亾dé卋堺 提交于 2019-12-01 11:10:18
Artur Udod

A wonderful solution using Linq by @pengyang that works with IEnumerables:

Private Shared Function GetCombinations(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
    If length = 1 Then
        Return list.[Select](Function(x) New T() {x})
    End If

    Return GetCombinations(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
End Function

Then:

Dim result = GetCombinations("abc", 3)

Screenshot from LinqPad: http://i.imgur.com/Xgjg9bz.png

Three nested loops

    Dim foo As String = "abc"
    For Each c1 As Char In foo
        For Each c2 As Char In foo
            For Each c3 As Char In foo
                Debug.WriteLine(String.Join("", c1, c2, c3))
            Next
        Next
    Next
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!