0242. Valid Anagram

# 242. Valid Anagram#

## 题目 #

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

``````
Input: s = "anagram", t = "nagaram"
Output: true

``````

Example 2:

``````
Input: s = "rat", t = "car"
Output: false

``````

Note:

You may assume the string contains only lowercase alphabets.

What if the inputs contain unicode characters? How would you adapt your solution to such case?

## 代码 #

``````
package leetcode

// 解法一
func isAnagram(s string, t string) bool {
alphabet := make([]int, 26)
sBytes := []byte(s)
tBytes := []byte(t)
if len(sBytes) != len(tBytes) {
return false
}
for i := 0; i < len(sBytes); i++ {
alphabet[sBytes[i]-'a']++
}
for i := 0; i < len(tBytes); i++ {
alphabet[tBytes[i]-'a']--
}
for i := 0; i < 26; i++ {
if alphabet[i] != 0 {
return false
}
}
return true
}

// 解法二
func isAnagram1(s string, t string) bool {
hash := map[rune]int{}
for _, value := range s {
hash[value]++
}
for _, value := range t {
hash[value]--
}
for _, value := range hash {
if value != 0 {
return false
}
}
return true
}

``````

Apr 8, 2023