LeetCode 49: Group Anagrams | Sort and Hash

preview_player
Показать описание
0:00 Intro
0:47 Solution
1:48 Code walkthrough

Рекомендации по теме
Комментарии
Автор

This is an O( N * M log M ) solution, where N is the number of strings and M is the length of a single string . You can do better . Avoid sorting and create an array of size 26, every string will have a unique signature .

Mapping using that concept will lead to the solution to be O ( N * M ), with 26 * O ( N * M ) space


Also, using a defaultdict( list) datastructure is python can make it's implementation simpler

vnnyboy
Автор

oh lol. i saw this video, thought id solve it before watching . this exactly how i did it. i think this would be the most common solution i guess
The ohnly addition I would say. dicts have a `get` function that can return a default. You can do
```
my_list = group.get(sorted_word, [])
my_list.append(word)
my_list[sorted_word] = my_list
````
if sorted_word is not seen so far, it will return an empty list as default

pvic