I like how you include data structures and algorithms in your tutorials rather than just throwing the easiest solution.
@LucidProgramming5 жыл бұрын
Thank you! I appreciate that comment. I hope these videos are helpful, and thanks again for watching!
@goldy58584 жыл бұрын
First, thanks a lot for this algorithm course. I am not from CS background but your course is helping me a lot to understand the basics. For the example, I think the two 'if' statements can be replaced by one 'if' to search for every letter of string two is in the hash table or not. If not, we can return false else true. No need for subtraction and checking for zeros.
@LucidProgramming4 жыл бұрын
Sure, you could do that to simplify the code slightly. Cheers, and thanks for watching!
@gevorggalstyan63924 жыл бұрын
Beautiful solution. Thank you for all your videos.
@LucidProgramming4 жыл бұрын
Cheers, and thanks for watching!
@aghiadalzein30694 жыл бұрын
I was wondering whether on the second for loop if the letter doesn't exist in ht we can return False directly(and save us from entering the third for loop) ,is not that right ??!! Best regards.
@LucidProgramming4 жыл бұрын
Sure, you could.
@MattWiener13 жыл бұрын
I came to the comments to look for this. if i is not in ht for s2, false. once you finish checking s2 you know you're done.
@mahammadodj4 жыл бұрын
thank you so much
@LucidProgramming4 жыл бұрын
No problem, thanks for watching!
@finnley-71882 жыл бұрын
Hey do you know how to make this into a function
@LucidProgramming2 жыл бұрын
It's already in a function, is it not?
@DHAiRYA28014 жыл бұрын
def anagram(a,b): score = 0 for i in a: if i in b: score+=1 return score==len(a) What is the problem here?
@LucidProgramming3 жыл бұрын
Seems fine to me, don't see a problem.
@pradeepkumar-qo8lu5 жыл бұрын
Why not use sorted(s1)==sorted(s2) after processing the strings Don't they achieve the same thing or am I missing something??
@LucidProgramming5 жыл бұрын
Hi Pradeep. You could do that, but that would give you a hit in time and space complexity. Does the achieve the same thing though!
@pythonenthusiast92924 жыл бұрын
In the second for loop , why is it important to put the else condition ? as if the element is already not present in the dictionary then it is a new element and we can return false there itself right??
@LucidProgramming4 жыл бұрын
But we need to set it back to being equal to "1", right?
@marcobroca40204 жыл бұрын
Nice video, thanks for taking time to do such a great job. I have a doubt, could also set() function be a solution? since it has a time cmplexity of o(1). def anagram(text1, text2): text1, text2 = [i for i in text1.lower() if i.isalpha()], [i for i in text2.lower() if i.isalpha()] if len(text1)==len(text2) and set(text1)==set(text2): return True return False anagram("fairy tales? ", "Rail safety")
@LucidProgramming4 жыл бұрын
There is no way that set has a complexity of O(1).
@brendancheong77383 жыл бұрын
what is the time complexity of this algorithm? (since its faster than n log(n))
@LucidProgramming3 жыл бұрын
Do you have any guesses?
@brendancheong77383 жыл бұрын
@@LucidProgramming Im gonna go on a wild guess and say O(n)
@LucidProgramming3 жыл бұрын
@@brendancheong7738 Yeah, both for time and space.
@linusjohansson31644 жыл бұрын
Great videos! Subd! Can you recommend a good source where i can learn about time and space of algorithms, that comparison based sorting algorithms requries at least n log n time to sort etc?
@LucidProgramming4 жыл бұрын
Thanks! And yeah, MIT opencourseware has a great course on algorithms that would be right up your alley!
@linusjohansson31644 жыл бұрын
Thank you! @@LucidProgramming
@LucidProgramming4 жыл бұрын
@@linusjohansson3164 Of course, and thank you for the support!
@KippiExplainsStuff5 жыл бұрын
Ok. Nice solution. Question: Is it not even better (time wise) to initialize the dict by the 26 letters of the alphabet and the value as zero? This will cut down 2 if statements. Whenever you encounter a letter you simply do ht[i] +=1 No matter if the letter has been previously found.
@vincentrusso65635 жыл бұрын
Time-wise, that does seem to be beneficial, but in space you'll be taking on some extra when you initialize the dictionary. For this problem on such a small size, it's not a big deal of course, but for larger problem sizes it might be a factor you're concerned about. Cheers, and thanks as always for your comments!
@KippiExplainsStuff5 жыл бұрын
@@vincentrusso6563 Yes. Space might be an issue. Good point. My thought is, with modern computing, where space is almost never an issue anymore (unless you are working on RT systems with tiny RAM or something), I'd rather "waste" a little space rather than time. Obviously this should be viewed on a case to case question.
@LucidProgramming5 жыл бұрын
@@KippiExplainsStuff Right, good point. Surely, especially for this problem, space will not be an issue. However, in an interview setting, it is good to point out that you are aware of the space requirements especially if they happened to be much larger than this particular example. Thanks for the comment, as always!
@shonnoronha4 жыл бұрын
How about this we take s1 and s2 as input create and empty list l1. Loop through s1 if i in s1 and i also in s2 . Append to l1. If the length of l1 and s1 or s2 matches return True .if lenth does not match or both are empty string return False. If it's wrong please correct me!
@LucidProgramming4 жыл бұрын
The general idea is fine, but the runtime of your solution is worse.
@shonnoronha4 жыл бұрын
@@LucidProgramming Thanks for replying, I'm just a beginner with no prior knowledge of programming, going through your playlist ,your videos are really good ! Keep going !
@LucidProgramming4 жыл бұрын
@@shonnoronha Cheers! If you enjoyed and benefited from my content, please consider liking the video and subscribing to the channel for more content like this. If you would like to support the content creation on this channel please consider unblocking ads when watching my videos as this is how I support my time to make content. I hope to be putting out more similar videos soon!
@larrybai5 жыл бұрын
In is_anagram() for i in s2: if i in ht: ht[i]=ht[i]-1 else: return False #other than ht[i]=1 Because if i is in s2 but not in s1, we can determine that s1 and s2 are not anagram
@LucidProgramming5 жыл бұрын
I think there are some edge cases for which this doesn't work.
@rizetothemoon5 жыл бұрын
@@LucidProgramming I had the same thought as @Lei Zhang. If s2 contains a char. which has not been found in s1 (having an entry in the hash table), can't we automatically conclude s2 is not an anagram of s1? Do you mind sharing those edge cases?
@LucidProgramming5 жыл бұрын
@@rizetothemoon Ah okay, I think I understand what Lei was proposing. Yes, of course, you can have an early exit condition like the one you are describing there, I don't see any issue with that. Sorry for the confusion!
@rizetothemoon5 жыл бұрын
@@LucidProgramming thanks for the reply. I have watched all your data structure and algo videos. Appreciate the work you've done!
@LucidProgramming5 жыл бұрын
@@rizetothemoon Cheers, I appreciate that, Roger!
@jillward6256 Жыл бұрын
Kodie an Kyler should be ok to use as an anagr
@LucidProgramming Жыл бұрын
You can use them to check, but those words would not be anagrams.
@mohammedrilwan90504 жыл бұрын
we can return false if 'i not in ht'..while iterating in S2..isnt it ?