From 9bd8221089ca3cd23c1fe98145041d4384313daa Mon Sep 17 00:00:00 2001 From: rizzOn Date: Sat, 6 Jun 2026 01:11:24 +0530 Subject: [PATCH] day 4: rizzzzzzzzzzzzzzzzzzzz --- grouping_anagrams.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 grouping_anagrams.go diff --git a/grouping_anagrams.go b/grouping_anagrams.go new file mode 100644 index 0000000..c384cb3 --- /dev/null +++ b/grouping_anagrams.go @@ -0,0 +1,23 @@ +package gopherriz + +import "sort" + +// Given a list of words, group together +// those that are anagrams of each other ("eat", "tea", "ate" belong together). + +func groupAnagrams(words []string) [][]string { + groups := make(map[string][]string) + + for _, word := range words { + str := []byte(word) + sort.Slice(str, func(i, j int) bool { return str[i] < str[j] }) + groups[string(str)] = append(groups[string(str)], word) + } + + result := make([][]string, len(groups)) + for _, group := range groups { + result = append(result, group) + } + return result + +}