Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created December 18, 2013 00:04
Show Gist options
  • Select an option

  • Save whyrusleeping/8015116 to your computer and use it in GitHub Desktop.

Select an option

Save whyrusleeping/8015116 to your computer and use it in GitHub Desktop.
Get all permutations of a set of strings
func permute(opts []string) []string {
return _permute("", opts)
}
func _permute(s string, opts []string) []string {
if len(opts) == 0 {
return []string{s}
}
ret := make([]string, 0, 16)
olist := make([]string, 0, len(opts))
for i := 0; i < len(opts); i++ {
a := s + opts[i]
olist = olist[:0]
for j := 0; j < len(opts); j++ {
if j != i {
olist = append(olist, opts[j])
}
}
ret = append(ret, _permute(a, olist)...)
}
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment