Skip to content

Instantly share code, notes, and snippets.

@kristofer
Last active October 13, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristofer/1a54cb5fdd2a2b3fcc50cf8c29291f13 to your computer and use it in GitHub Desktop.
Save kristofer/1a54cb5fdd2a2b3fcc50cf8c29291f13 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
const (
string1 = "aab"
string2 = "aba"
string3 = "cab"
string4 = "kjfdhskdhfksdhk"
)
func anagram(x, y string) bool {
if x == y {
return true
}
if len(x) != len(y) {
return false
}
dictx := make(map[string]int)
dicty := make(map[string]int)
for i := 0; i < len(x); i++ {
dictx[string(x[i])] += 1
}
for i := 0; i < len(y); i++ {
dicty[string(y[i])] += 1
}
if len(dictx) != len(dicty) {
return false
}
for k, _ := range dictx {
if dictx[k] != dicty[k] {
return false
}
}
return true
}
func main() {
fmt.Printf("%v\n", anagram(string1, string1))
fmt.Printf("%v\n", anagram(string1, string2))
fmt.Printf("%v\n", anagram(string1, string3))
fmt.Printf("%v\n", anagram(string1, string4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment