Problem Link: https://leetcode.com/problems/valid-anagram/
Solution:
func isAnagram(s string, t string) bool {
var n1 = len(s)
var n2 = len(t)
if n1 != n2 {
return false
}
var data1 = make(map[byte]int)
var data2 = make(map[byte]int)
for i := 0; i < n1; i++ {
var c1 = s[i]
var c2 = t[i]
_, ok1 := data1[c1]
_, ok2 := data2[c2]
if ok1 {
data1[c1]++
} else {
data1[c1] = 1
}
if ok2 {
data2[c2]++
} else {
data2[c2] = 1
}
}
for k := range data1 {
if data1[k] != data2[k] {
return false
}
}
return true
}
Top comments (0)