How to print all possible permutations (all possible orderings) of the words in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Module Module1

    Sub PermutationsOfWords(words As List(Of String), current As List(Of String))
        If words.Count = 0 Then
            Console.WriteLine(String.Join(" ", current))
            Return
        End If

        For i As Integer = 0 To words.Count - 1
            Dim remaining As New List(Of String)(words)
            remaining.RemoveAt(i)

            Dim nextList As New List(Of String)(current)
            nextList.Add(words(i))

            PermutationsOfWords(remaining, nextList)
        Next
    End Sub

    Sub Main()
        Dim words As New List(Of String) From {"word-1", "word-2", "word-3"}
	
        PermutationsOfWords(words, New List(Of String))
    End Sub

End Module

 
 
' run:
'
' word-1 word-2 word-3
' word-1 word-3 word-2
' word-2 word-1 word-3
' word-2 word-3 word-1
' word-3 word-1 word-2
' word-3 word-2 word-1
'

 



answered Sep 14, 2024 by avibootz
edited Apr 14 by avibootz

Related questions

...