【Leetcode】：242. Valid Anagram问题 in Go语言

u013564276 · · 1282 次点击 · · 开始浏览

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

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return 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?

```func isAnagram(s string, t string) bool {
if (len(s) != len(t)) {
return false
}
m := map[string]int{}
for _, v := range s {
key, ok := m[string(v)]
if !ok {
m[string(v)] = 1
} else {
m[string(v)] = key + 1
}
}
for _, v := range t {
key, ok := m[string(v)]
if !ok {
return false
}
if key == 0 {
return false
}
m[string(v)] = key - 1
}
return true
}```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

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

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return 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?

```func isAnagram(s string, t string) bool {
if (len(s) != len(t)) {
return false
}
m := map[string]int{}
for _, v := range s {
key, ok := m[string(v)]
if !ok {
m[string(v)] = 1
} else {
m[string(v)] = key + 1
}
}
for _, v := range t {
key, ok := m[string(v)]
if !ok {
return false
}
if key == 0 {
return false
}
m[string(v)] = key - 1
}
return true
}```