Last active
October 13, 2017 15:42
-
-
Save kristofer/1a54cb5fdd2a2b3fcc50cf8c29291f13 to your computer and use it in GitHub Desktop.
anagram-go-java Go see: https://gist.github.com/kristofer/57c68ded3438a9da76b454da0eb2d65d
This file contains hidden or 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 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