29 lines
584 B
Go
29 lines
584 B
Go
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
|
|
}
|