-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutations_iterative.go
52 lines (38 loc) · 1.11 KB
/
permutations_iterative.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import "fmt"
/*
Use permutations of n-1 elements to generate permutations of n elements.
Insert element n on each location of the n-1 permutation
Note that there are n! permutations
Example:
input: "abc"
iteration 1 result: "a"
iteration 2 result: "ab", "ba"
iteration 3 result: "abc", "acb", "cab", "bac", "bca", cba"
The method below is a waste of memory but it does work. Look into
Johnson and Trotter algorithm for the optimal way of doing this.
*/
func main() {
var result = permutations("abcd")
fmt.Println(result)
fmt.Println(len(result))
}
func permutations(input string) []string {
result := []string{string(input[0])}
for i := 1; i < len(input); i++ {
var tmp []string
for j := 0; j < len(result); j++ {
tmp = append(tmp, insert(result[j], string(input[i]))...)
}
result = make([]string, len(tmp))
copy(result, tmp)
}
return result
}
func insert(input string, char string) []string {
var result []string
for i := len(input); i >= 0; i-- {
result = append(result, input[:i]+char+input[i:])
}
return result
}