day 4: rizzzzzzzzzzzzzzzzzzzz

This commit is contained in:
rizzOn 2026-06-06 01:11:24 +05:30
parent 40632a8349
commit 9bd8221089
1 changed files with 23 additions and 0 deletions

23
grouping_anagrams.go Normal file
View File

@ -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
}