Valid Anagrams
Problem statement
Return whether s and t use exactly the same characters with the same frequencies (order may differ).
Example:
Input:
s = "abcd", t = "bcad"
Expected output:
true
Practice on LeetCode: Valid Anagram
Golang Solution
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
mapS := make(map[byte]int)
mapT := make(map[byte]int)
for i:=0; i<len(s); i++ {
if res, ok := mapS[s[i]]; ok {
mapS[s[i]] = res + 1
} else {
mapS[s[i]] = 1
}
if res, ok := mapT[t[i]]; ok {
mapT[t[i]] = res + 1
} else {
mapT[t[i]] = 1
}
}
for i:=0; i<len(s); i++ {
if mapS[s[i]] != mapT[s[i]] {
return false
}
}
return true
}
