I have this piece of code to find groups of anagrams as string arrays given an array of strings where n is the array length and m is the max string length.
func GroupAnagrams(strs []string) [][]string {
res := [][]string{}
m := map[[26]int][]string{}
for _, s := range strs {
a := [26]int{}
for _, uc := range s {
a[uc-'a']++
}
m[a] = append(m[a], s)
}
for _, v := range m {
res = append(res, v)
}
return res
}
I think the big O for the first loop is O(mn). But the algorithm has to loop through the map to add up the results. So what is the big O for that?
Side question, anyone have a faster solution for this lol?