Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,927 questions

51,860 answers

573 users

How to create permutations of words without repetition in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class PermutationsOfWordsWithoutRepetition_VB_NET
    Public Shared Sub Main()
        Dim words As String() = {"word-1", "word-2", "word-3", "word-4"}
        Dim result = GeneratePermutations(words, 0, words.Length - 1)

        For Each permutation In result
            Console.WriteLine(String.Join(" ", permutation))
        Next
    End Sub

    Public Shared Function GeneratePermutations(ByVal words As String(), ByVal start As Integer, ByVal [end] As Integer) As List(Of String())
        Dim result = New List(Of String())()

        If start = [end] Then
            result.Add(CType(words.Clone(), String()))
        Else
            For i As Integer = start To [end]
                Swap(words(start), words(i))
                result.AddRange(GeneratePermutations(words, start + 1, [end]))
                Swap(words(start), words(i))
            Next
        End If

        Return result
    End Function

	Public Shared Sub Swap(ByRef a As String, ByRef b As String)
        Dim temp As String = a
        a = b
        b = temp
    End Sub
End Class


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

 



answered Sep 14, 2024 by avibootz

Related questions

1 answer 100 views
2 answers 98 views
1 answer 90 views
1 answer 106 views
1 answer 89 views
...