LeetCode Problem: Valid Anagrams
Problem statement:
You are given two strings, check if they are anagrams or not
Example:
Input:
s = "abcd" t = "bcad"
Expected output:
true
If you would like to solve the problem on Leetcode, here is the link to the problem: https://leetcode.com/problems/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
}