Last active
March 9, 2017 09:21
-
-
Save alextanhongpin/8fe620f41fd48196bfe9ba283ffc6cda to your computer and use it in GitHub Desktop.
Test for isogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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