Check if two strings are anagrams

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

COMPLETE PLAYLIST
————————————

INTERVIEW PLAYLIST
————————————

QUICK SHORT VIDEOS
————————————-

In this video i have explained 3 different ways to solve this problem. This is very easy yet important question. This is generally asked in programming interview questions for placement. It can be best solved using hashing so interviewer ask this type of questions to check your knowledge on hashing.

Let me know if you feel something should be added in the comment section.

#anagram #dsa #c++ #interviewquestion
Рекомендации по теме
Комментарии
Автор

whats the time complexity and space complexity ?

Anikashukla
Автор

My python implementation:
def is_Anagram(texta, textb):
if len(texta) != len(textb):
return False

for char in texta:
if texta.count(char) != textb.count(char):
return False

return True

muhammadzakiahmad
Автор

or you can just add the ascii numbers and compare. say numbers a-z as 1-26 then string "car" will add up to 3+1+18 = 22 and "arc" will be the same.

pitrya
Автор

Can i use two for loops to count each word characters count and then return true if map1 == map2?

notpipa_
Автор

Hey buddy whr were u??
Please keep uploading videos...i really love your content...

SaurabhMishra
Автор

I guess we can avoid one additional loop, we can increment the count while iterating over first string and decrement the count of map for the second element. At the end we can iterate over the map if any all character has 0 count then it is anagram else it is not. Please correct me if I am wrong.

ChandraShekhar-bycd
Автор

How about having two sum variables and adding of ascii values for two string characters, if string length is same, in single for loop and comparing both sum are equal for an anagram

sairamsharma
Автор

I want to know if that last loop is required?

arpittripathi
Автор

Thank you for the great content. It is helping to prepare for interviews.
How can we make this solution, case insensitive. Currently it is case sensitive. If "Listen", "silent" - it's saying, not anagram. If "listen", "silent" - it's saying, anagram. Please suggest. We will have to check it's ascii value and convert ascii value to small case lettter asciis, I think and store that in the same map. Or at the beginning itself, convert both strings to lower case or upper case.
Added below lines and it's working fine.
#include <algorithm> //for transform function

transform(one.begin(), one.end(), one.begin(), ::tolower);
transform(two.begin(), two.end(), two.begin(), ::tolower);
//where one and two are string variables

pallavichaudhary
Автор

Thanks a lot for your informative video. 😃

ChandraShekhar-bycd
Автор

how to do for more than 2 words like
if cat tac act dog god
we have to get act tac cat
dog god

cheelavaishnavi