-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path49.字母异位词分组.go
62 lines (59 loc) · 1.22 KB
/
49.字母异位词分组.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import "sort"
/*
* @lc app=leetcode.cn id=49 lang=golang
*
* [49] 字母异位词分组
*
* https://leetcode-cn.com/problems/group-anagrams/description/
*
* algorithms
* Medium (55.80%)
* Likes: 141
* Dislikes: 0
* Total Accepted: 16.3K
* Total Submissions: 29.2K
* Testcase Example: '["eat","tea","tan","ate","nat","bat"]'
*
* 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
*
* 示例:
*
* 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
* 输出:
* [
* ["ate","eat","tea"],
* ["nat","tan"],
* ["bat"]
* ]
*
* 说明:
*
*
* 所有输入均为小写字母。
* 不考虑答案输出的顺序。
*
*
*/
func groupAnagrams(strs []string) [][]string {
m := make(map[string][]string, 0)
for _, str := range strs {
key := stringToKey(str)
if val, ok := m[key]; ok {
m[key] = append(val, str)
} else {
m[key] = []string{str}
}
}
result := make([][]string, 0)
for _, items := range m {
result = append(result, items)
}
return result
}
func stringToKey(str string) string {
bytes := []byte(str)
sort.Slice(bytes, func(i, j int) bool {
return bytes[i] < bytes[j]
})
return string(bytes)
}