Group Anagrams (LeetCode 49) | Full solution with 2 methods and examples | Study Algorithms

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

You are given an array of strings, you need to group the anagrams. Two strings are said to be anagrams if they have the exact same characters with the same frequency. The placement can change though. Watch this video to learn 2 methods to solve this problem. We will take advantage of properties of anagrams and use it in our favor. At the end we will see a dry-run of the code to see what is actually happening behind the scenes.

00:00 - Intro
01:12 - Problem statement and description
04:40 - Method 1: Categorize by Sorting
09:03 - Method 2: Categorize by Frequency
13:09 - Dry-run of Code
15:44 - Generate Frequency String
18:09 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

My Recording Gear:

💻 Get Social 💻

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

I generally not hit like on youtube videos, but this guy nailed it. Thanks for your community contribution NIkhil.

dhavalchaudhary
Автор

loved your explanation . But 1 thing I would like to correct . The time complexity of method 1 will be O(n k log k ), where k is the length of the string, n is total strings . This is because sorting a string would take (k log k ) . Any way awesome explanation . thanks

devprakash
Автор

I was missing ONE idea and it popped in my head when I saw your first method, amazing explanation!

WhosShamouz
Автор

I got really surprised by so a vivid and clear explanation. I appreciate your efforts.

amitbhattacharya
Автор

sir you explaination to any algorithm is the best explanation i had ever seen ...only your explanation is enough to solve the problem no need to see the code

vikashsharma
Автор

Dhanyavad bhaiya, you are the best teacher I have seen on youtube😭

plutomessi
Автор

At the beginning it was difficult to find out how to solve this problem, but with this explanation it make it easy to solve it. Thank.

ricardohernandezmendez
Автор

Thank you, you explained it very well!

ElinaAdibi-bt
Автор

In the sorting way, the time complexity is not O(nlogk) but rather O(n.klogk)

akshanshsharma
Автор

Hey, I found this video trough the article and thank you for all the work, it really helped me. The solution of method 2 at line 34 has a mistake: "freq++;", you can't ++ an array, so we need to do freq[c - 'a']++ :)

Donkle
Автор

i'm new to python and this was my solution

def smash(str):

tmp = 1
orda = ord('A')-1
ls = len(str)

for i in range( ls ):
tmp = tmp * (ord(str[i]) - orda)

return tmp


def groop(wordlist):

grope = {}

for word in wordlist:
hashy = smash(word)
if hashy not in grope:
grope[hashy] = []

grope[hashy].append(word)

return [val for val in grope.values()]

if __name__ == '__main__':
# arr = [ 'cat', 'tea', 'tan', 'ate', 'nat', 'bat' ]
arr = [ 'eat', 'cars', 'tea', 'scar', 'a', 'listen', 'silent']

print (groop(arr))

mazthespaz
Автор

bro, thank you for the video. it was very usefull (like all your videos) <3

edd_gos
Автор

Very well explained the techniques, code and dry-run. Great work Nikhil.

shaileshsathe
Автор

We need to write getfrequency string string method also sep?

lofiboy
Автор

great video and best ever explaination ever

RohitSharma-qjp
Автор

If I use sorting logic will be my solution wrong?
I am facing difficulty in understanding the HasMap and Frequency Concept.

shahbazhussain
Автор

great explanation with animation step by step

ramsidh
Автор

That's an amazing explanation! 👏👏

hersheynohara
Автор

8:14 should be "nat", not sorted string "ant".
Otherwise your tutorials are the best regarding leetcode problems, because you explain with examples what should the code do.

jst
Автор

this there any better way for a frequency string algothrim?

yfchina
visit shbcf.ru