Group Anagrams - Leetcode 49 - Hashmaps & Sets (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Watched neetcode and some other videos on this problem and they sped through all of this code without properly explaining why we are doing what were doing. Happy to have found this video. Really solid explanation. Keep it up

swiftlyr
Автор

just found this channel. your amazing man.

moezzzz
Автор

slight note. return statement should be since the question asks to return a list of a list and not dict_values. great solution anyways

teostorm
Автор

I've done like this
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
counter = defaultdict(list)
for s in strs:
key = ''.join(sorted(s))
counter[key].append(s)
return list(counter.values())

anandreddy
Автор

fantastic example to help learner to realize that dict and sets are not hashable, since almost everyone would have thought of using a set of sets (i did) or dict with keys as dict (like you did) and realize that it's not possible

jmgpqcx
Автор

I love the solution. I am curious as to why using dict is faster than defaultdict

benjaminappiah
Автор

im confused about if the key isn't in the dictionary yet, it associates its value with an empty list. how does the first string in a list get added when the key isn't in the dictionary yet if it associates an empty list and not the appended string? hope this makes sense

ariellockett
Автор

What’s the problem converting dict to str and pass it as a key?

matveysaprykin
Автор

my solution ..runtime 87 19.3 MB
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_dict={}
for s in strs:
sorted_s= "".join(sorted(s))
if sorted_s in anagram_dict:

else:
anagram_dict[sorted_s]=[s]

return list(anagram_dict.values())

FUNTasticFlutter
Автор

4:58 Note : ord is used to get the unicode of a charecter and not ascii value

Ak-ggs
Автор

I don't think I'd need to learn this one ... lol

hlubradio
welcome to shbcf.ru