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
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.
@@CppNuts we can delete key from map if after decrement Map[b[I]] becomes zero last loop can be replaced with: return Map.empty(); this method will be faster because decrement loop can break if there is no more that symbol in Map
@@CppNuts Since both string will be having same length , in that same for lopp we can increment the count of character from string 1 and decrement the count of the char for the other string.
Got it, yes that is correct, but in last solution we don't need 3rd loop that's what i asked in the end of the video. So in the end we have 2 loops in both the solutions. Good for you to think this much for given problem, then only you guys can get the max benefits.
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
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 //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
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
whats the time complexity and space complexity ?
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.
what will be the time complexity and space complexity for this?
No , 10+2 = 8+4 = 12 for eg. This will lead to incorrect results
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.
How r u populating Map which you are decrementing?
@@CppNuts we can delete key from map if after decrement Map[b[I]] becomes zero
last loop can be replaced with: return Map.empty();
this method will be faster because decrement loop can break if there is no more that symbol in Map
@@CppNuts Since both string will be having same length , in that same for lopp we can increment the count of character from string 1 and decrement the count of the char for the other string.
Got it, yes that is correct, but in last solution we don't need 3rd loop that's what i asked in the end of the video.
So in the end we have 2 loops in both the solutions.
Good for you to think this much for given problem, then only you guys can get the max benefits.
Boss is Back 😎
Thanks hahaha
Hey buddy whr were u??
Please keep uploading videos...i really love your content...
Thanks man, sure I’ll do.
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
Hi Shriram, No this wont work.
@@CppNuts Yeah true👍 my bad one such example is "ac" & "bb"...thanks for reply!
Can i use two for loops to count each word characters count and then return true if map1 == map2?
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 //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
Converting in the beginning will be very easy, if interviewer is ok with that then grt, otherwise check what he/she wants in this case.
Thanks a lot for your informative video. 😃
Thanks man!!
I want to know if that last loop is required?
Just think…
@@CppNuts I think it is not required. It can be avoided as the conditions above last loop cover all possible scenarios
Correct..
wonderful video
After long time
Yes next is vptr and vtable.
Or i will complete this series first then will go for other topics.
@@CppNuts please make video for dll and move constructor
An important video.
Thanks
how to do for more than 2 words like
if cat tac act dog god
we have to get act tac cat
dog god