Leetcode - Group Anagrams
https://leetcode.com/problems/group-anagrams/
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
1. Count frequency of letters in every word with count sort
2. Iterate the count from the beginning and compose another sorted string
3. Use a hash, key is sorted string, value is collecting original words
func groupAnagrams(strs []string) [][]string {
count := make([]int, 26)
hash := map[string][]string{}
for _, w := range strs {
for i := 0; i < len(w); i++ {
count[w[i]-'a']++
}
str := []byte{}
for i := 0; i < 26; i++ {
if count[i] != 0 {
val := count[i]
for val > 0 {
str = append(str, byte(i)+'a')
val--
}
count[i] = 0
}
}
hash[string(str)] = append(hash[string(str)], w)
}
result := [][]string{}
for _, val := range hash {
result = append(result, val)
}
return result
}
Dec 06, 2021 01:40:36 PM
Wow goodness! What inconceivable substance post by you on this site. Alwarpet Call Girls || call girl in Anna Nagar || Call Girls Service in Choolai || Harrington Road Call girls || Call Girls in Egmore || Escorts Service in Chennai
Escorts Service in Chennai Escorts Service in Chennai really it is an unprecedented thing for your substance.
Dec 06, 2021 01:44:20 PM
Very helpful and informative massage posting on your site for us. Chennai Escort Chennai Escorts Chennai Escorts Service Escorts in Chennai Sexy Escorts in Chennai I am read yor blog when you post.