原题
https://leetcode.cn/problems/valid-anagram/description/
解法
(针对进阶场景,若字符串中存在unicode字符)
func isAnagram(s string, t string) bool {
if len(s)!=len(t){
return false
}
var letterList []rune
letterCountS := map[rune]int{}
letterCountT := map[rune]int{}
for _, c := range s{
count := letterCountS[c]
if count == 0{
letterList = append(letterList, c)
}
letterCountS[c] = count + 1
}
for _, c := range t{
if letterCountS[c] == 0{
return false
}
letterCountT[c] ++
}
for _, c := range letterList{
if letterCountS[c] != letterCountT[c]{
return false
}
}
return true
}
原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode242-%e6%9c%89%e6%95%88%e7%9a%84%e5%ad%97%e6%af%8d%e5%bc%82%e4%bd%8d%e8%af%8d/