' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System

Class MainClass

    Private Shared Sub FillAndCheck(ByVal s As String, ByVal ix As Integer, ByVal actlen As Integer, ByVal charset As Char(), ByVal used As Boolean(), ByVal target As String)
        If ix = actlen Then
            If s = target Then
                Console.WriteLine(target + " found")
            End If
        Else
            Dim tmp As String
            Dim i As Integer
            For i = 0 To charset.Length-1
                If Not used(i) Then
                    used(i) = True
                    tmp = s + charset(i)
                    FillAndCheck(tmp, ix + 1, actlen, charset, used, target)
                    used(i) = False
                End If
            Next
        End If
    End Sub
    Private Shared Sub FindOneLength(ByVal target As String, ByVal actlen As Integer, ByVal charset As String)
        Dim used(charset.Length - 1) As Boolean
        Dim j As Integer
        For j = 0 To used.length-1
            used(j) = False
        Next
        FillAndCheck("", 0, actlen, charset.ToCharArray, used, target)
    End Sub
    Public Shared Sub Find(ByVal target As String, ByVal maxlength As Integer, ByVal charset As String)
        Dim i As Integer
        For i = 1 To maxlength
            FindOneLength(target, i, charset)
        Next
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Find("bc", 3, "abcd")
        Find("xy", 3, "abcd")
        Find("d", 3, "abcd")
        Find("dcba", 3, "abcd")
    End Sub
End Class 
