How to print all possible permutations (all possible orderings) of the words in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func permutations_of_words(arr []string, left, right int) {
    if left == right {
        fmt.Println(strings.Join(arr, " "))
    } else {
        for i := left; i <= right; i++ {
            arr[left], arr[i] = arr[i], arr[left] // swap
            permutations_of_words(arr, left + 1, right)
            arr[left], arr[i] = arr[i], arr[left] // backtrack
        }
    }
}

func main() {
    arr := []string{"Go", "Programming", "Language"}
    
    permutations_of_words(arr, 0, len(arr) - 1)
}



/*
run:

Go Programming Language
Go Language Programming
Programming Go Language
Programming Language Go
Language Programming Go
Language Go Programming

*/

 



answered Jan 20, 2025 by avibootz

Related questions

...