Print all permutations of a string in golang

Go program for Print all permutations of a string. Here more information.

package main
import "fmt"
// Go program for
// Print all permutations of a string

// Method which is print all permutations of given string
func showPermutation(text []rune, 
	n int, size int) {
	if n > size {
		return
	}
	if n == size {
		fmt.Println(string(text))
		return
	}
	for i := n ; i < size ; i++ {


		// Swap the element
		text[i],text[n] = text[n],text[i]

		showPermutation(text, n + 1, size)

		// Swap the element
		text[i],text[n] = text[n],text[i]
	}
}
func findPermutation(text string) {
	var size = len(text)
	r := []rune(text)
	showPermutation(r, 0, size);
}

func main() {
	
	var text string = "ABC"
	findPermutation(text)
	fmt.Println()
	text = "abcd"
	findPermutation(text)
}

Output

ABC
ACB
BAC
BCA
CBA
CAB

abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved