File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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\n b=%v\n " , a , b )
18+
19+ return a == b
20+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments