Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Last active March 9, 2017 09:21
Show Gist options
  • Save alextanhongpin/8fe620f41fd48196bfe9ba283ffc6cda to your computer and use it in GitHub Desktop.
Save alextanhongpin/8fe620f41fd48196bfe9ba283ffc6cda to your computer and use it in GitHub Desktop.
Test for isogram
package isogram
import (
"strings"
)
func IsIsogram(s string) bool {
if s == "" {
return false
}
// Create a map to store the unique characters
d := make(map[rune]int)
for _, r := range strings.ToLower(s) {
// Set unique characters
d[r] += 1
}
// Now we have a map of the unique characters
// In order to get an Array of the keys, we loop
var o []string
for k, _ := range d {
o = append(o, string(k))
}
// Array `o` now contains the unique strings
// Check how many times the strings are repeated
repeat := int(len(s) / len(o))
// Now check if each unique characters have the same amount
for _, v := range d {
if v != repeat {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment