Skip to content

Commit fe7e0d4

Browse files
authored
Merge pull request algorithm007-class02#343 from AndrewOYLK/master
0032_week02(go)
2 parents 2b5189e + 8ff320b commit fe7e0d4

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g20200343040032
2+
3+
import "fmt"
4+
5+
func isAnagram(s string, t string) bool {
6+
a := [26]int{}
7+
b := [26]int{}
8+
9+
for _, v := range s {
10+
a[v-'a'] += 1
11+
}
12+
13+
for _, v := range t {
14+
b[v-'a'] += 1
15+
}
16+
17+
fmt.Printf("a=%v\nb=%v\n", a, b)
18+
19+
return a == b
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g20200343040032
2+
3+
import "sort"
4+
5+
func groupAnagrams(strs []string) [][]string {
6+
if strs == nil || len(strs) == 0 {
7+
return [][]string{}
8+
}
9+
rMap := make(map[string][]int, 0)
10+
for i, v := range strs {
11+
str := aSort(v)
12+
if _, ok := rMap[str]; !ok {
13+
rMap[str] = []int{i}
14+
} else {
15+
rMap[str] = append(rMap[str], i)
16+
}
17+
}
18+
res := make([][]string, 0)
19+
for _, v := range rMap {
20+
a := make([]string, 0)
21+
for _, x := range v {
22+
a = append(a, strs[x])
23+
}
24+
res = append(res, a)
25+
}
26+
return res
27+
}
28+
29+
func aSort(s string) string {
30+
x := []byte(s)
31+
sort.Slice(x, func(i, j int) bool { return x[i] < x[j] })
32+
return string(x)
33+
}

0 commit comments

Comments
 (0)