Golang program to compute all the permutations of the string


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. A permutation of a string is a distinct string with the same characters but a different ordering of the characters. For instance, the words "abcd" and "dabc" are combinations of one another. Here, in this program we will explore method to find permutation of string and print the output on the console using print statement in Golang.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − The input list, its length, and a pointer to an array of integer slices that contains the permutations are passed to the heapPermutation function by the main function.

  • Step 3 − The heapPermutation function verifies that the list's length is 1. If it is, we have fixed all of the list's components, making the present list a permutation. As a result, it adds the current list to the permutation slice.

  • Step 4 − If the list's length is more than 1, the function enters a loop in which it continuously calls itself with the list's n-1 entries.

  • Step 5 − The last element of the list is swapped with each of the preceding components inside the loop using a nested loop.

  • Step 6 − The current permutation is then added to the permutations slice by the function.

  • Step 7 − The function then determines whether the list's length is even or odd using an if statement. If it is odd, it will swap the first element with the final element; if it is even, it will swap the element at the ith index with the last element.

  • Step 8 − After that, the function repeats the aforementioned procedure for each element in the list.

  • Step 9 − Following the generation of all possible permutations, the function returns the slice of permutations.

  • Step 10 − The permutation will be printed on the console using fmt.Println() function where ln means new line.

  • Step 11 − This approach has an O(n!) time complexity since there are n! permutations for a list of length n. Since all the permutations must be stored in the slice, the space complexity is also O(n!). Due to its non-recursive nature, this strategy may be more effective for big inputs than recursive approaches because it prevents stack overflow errors.

Example

Here we creates all lexicographically ordered permutations of a given list using the Heap's algorithm.

package main
import (
   "fmt"
)

func heapPermutation(mystr []int, n int, permutations *[][]int) {
   if n == 1 {
      *permutations = append(*permutations, append([]int{}, mystr...))
      return
   }
   for i := 0; i < n; i++ {   //calculate the permutation using heap
      heapPermutation(mystr, n-1, permutations)
      if n%2 == 0 {
         mystr[i], mystr[n-1] = mystr[n-1], mystr[i]
      } else {
         mystr[0], mystr[n-1] = mystr[n-1], mystr[0]
      }
   }
}

func main() {
   mystr := []int{10, 20, 30}  //create a slice with numbers whose permutation is to be calculated 
   fmt.Println("The elements of string are:", mystr)
   permutations := [][]int{}
   heapPermutation(mystr, len(mystr), &permutations)
   fmt.Println("The permutations of string presented here is:")
   fmt.Println(permutations)  //print the permutations 
}

Output

The elements of string are: [10 20 30]
The permutations of string presented here is:
[[10 20 30] [20 10 30] [30 10 20] [10 30 20] [20 30 10] [30 20 10]]

Conclusion

We executed the program of finding permutations of a string using one example. In the particular example we used heap algorithm to Carry on the execution. The program gave as expected output.

Updated on: 20-Feb-2023

595 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements