LeetCode

Problem Statement

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Solution

import (
	"sort"
	"strings"
)

func SortString(w string) string {
	s := strings.Split(w, "")
	sort.Strings(s)
	return strings.Join(s, "")
}

func groupAnagrams(strs []string) [][]string {
	m := make(map[string][]string)

	for _, w := range strs {
		word := SortString(w)
		m[word] = append(m[word], w)
	}

	var ss [][]string
	for e := range m {
		ss = append(ss, m[e])
	}
	return ss
}