exercism/go/anagram/anagram.go

29 lines
584 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
package anagram
import "strings"
// Detect returns all candidates that are anagrams of subject
func Detect(subject string, candidates []string) []string {
var ret []string
for i := range candidates {
if isAnagram(subject, candidates[i]) {
ret = append(ret, strings.ToLower(candidates[i]))
}
}
return ret
}
func isAnagram(s, c string) bool {
s = strings.ToLower(s)
c = strings.ToLower(c)
if s == c || len(s) != len(c) {
return false
}
for _, v := range strings.Split(s, "") {
if strings.Count(s, v) != strings.Count(c, v) {
return false
}
}
return true
}