Skip to content

Instantly share code, notes, and snippets.

@p4tin
Created June 21, 2018 03:51
Show Gist options
  • Save p4tin/d2073e7e3ec7abd815aa2083bc30163c to your computer and use it in GitHub Desktop.
Save p4tin/d2073e7e3ec7abd815aa2083bc30163c to your computer and use it in GitHub Desktop.
Find words that have the same letters in a list
package main
import (
"fmt"
"strings"
)
func rotate(a []byte, i int) {
for count := 1; count <= i; count++ {
tmp := a[0]
for n := 1; n < len(a); n++ {
a[n-1] = a[n]
}
a[len(a)-1] = tmp
}
}
func main() {
output := make([][]string, 0)
var input = []string{"Tokyo", "London", "Rome", "Donlon", "Kyoto", "Paris"}
for i := 0; i < len(input); i++ {
for j := i + 1; j < len(input); j++ {
if len(input[i]) != len(input[j]) {
continue
}
old := []byte(input[j])
for x := 0; x < len(input[j]); x++ {
rotate(old, 1)
if strings.ToLower(input[i]) == strings.ToLower(string(old)) {
fmt.Printf("%s == %s\n", input[i], input[j])
output = append(output, []string{input[i], input[j]})
continue
}
}
}
}
fmt.Println(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment