How to find permutation and combination of string in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to find permutation and combination of string using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
In this program I have replaced the angled brackets with > or <. once you copy the source code to editor replace it with greater than or less than symbols respectively.
##############SOURCE CODE#########################
package main
import (
"fmt"
"strings"
)
func permute(s string) []string {
// Base case: If the input string is empty, return an empty slice
if len(s) == 0 {
return []string{""}
}
// Initialize an empty slice to hold the permutations
permutations := []string{}
// Loop through each character in the input string
for i := 0; i < len(s); i++ {
// Remove the current character from the string
remainingChars := s[:i] + s[i+1:]
// Recursively generate permutations of the remaining characters
subPermutations := permute(remainingChars)
// Append the current character to each sub-permutation
for _, subPermutation := range subPermutations {
permutation := string(s[i]) + subPermutation
permutations = append(permutations, permutation)
}
}
return permutations
}
func combinations(s string) []string {
// Initialize an empty slice to hold the combinations
combinations := []string{}
// Loop through each character in the input string
for i := 0; i < len(s); i++ {
// Add the current character to the combinations slice
combinations = append(combinations, string(s[i]))
// Generate combinations of the remaining characters
for j := i + 1; j < len(s); j++ {
combinations = append(combinations, string(s[i])+string(s[j]))
}
}
return combinations
}
func main() {
// Define the input string
s := "abcd"
// Find the permutations of the input string
permutations := permute(s)
// Print the permutations to the console
fmt.Println("Permutations:")
for _, permutation := range permutations {
fmt.Println(permutation)
}
// Find the combinations of the input string
combinations := combinations(s)
// Print the combinations to the console
fmt.Println("Combinations:")
for _, combination := range combinations {
fmt.Println(combination)
}
}
##############END OF SOURCE CODE#########################
Below is the explanation for the program:
In this program, we define the input string s as "abcd". We then define two functions: permute and combinations.
The permute function recursively generates all possible permutations of a given string s.
We start by checking if the input string is empty. If it is, we return an empty slice to signal the end of the recursion.
If the string is not empty, we loop through each character in the string and remove it from the string, generating permutations of the remaining characters.
We then append the current character to each sub-permutation and add it to the list of permutations.
The combinations function generates all possible combinations of a given string s.
We loop through each character in the string and add it to the list of combinations.
We then loop through the remaining characters and add combinations of the current character and each subsequent character to the list.
In the main function, we call the permute and combinations functions with the input string s, and print the resulting permutations and combinations to the console.
#go #goprogramming #golang #golangtutorial #golanguage