String Processing in Python: Is Anagram

  Рет қаралды 15,125

LucidProgramming

LucidProgramming

Күн бұрын

Пікірлер: 50
@MasteringProgrammingTapAway
@MasteringProgrammingTapAway 5 жыл бұрын
I like how you include data structures and algorithms in your tutorials rather than just throwing the easiest solution.
@LucidProgramming
@LucidProgramming 5 жыл бұрын
Thank you! I appreciate that comment. I hope these videos are helpful, and thanks again for watching!
@goldy5858
@goldy5858 4 жыл бұрын
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.
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Sure, you could do that to simplify the code slightly. Cheers, and thanks for watching!
@gevorggalstyan6392
@gevorggalstyan6392 4 жыл бұрын
Beautiful solution. Thank you for all your videos.
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Cheers, and thanks for watching!
@aghiadalzein3069
@aghiadalzein3069 4 жыл бұрын
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.
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Sure, you could.
@MattWiener1
@MattWiener1 3 жыл бұрын
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.
@mahammadodj
@mahammadodj 4 жыл бұрын
thank you so much
@LucidProgramming
@LucidProgramming 4 жыл бұрын
No problem, thanks for watching!
@finnley-7188
@finnley-7188 2 жыл бұрын
Hey do you know how to make this into a function
@LucidProgramming
@LucidProgramming 2 жыл бұрын
It's already in a function, is it not?
@DHAiRYA2801
@DHAiRYA2801 4 жыл бұрын
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?
@LucidProgramming
@LucidProgramming 3 жыл бұрын
Seems fine to me, don't see a problem.
@pradeepkumar-qo8lu
@pradeepkumar-qo8lu 5 жыл бұрын
Why not use sorted(s1)==sorted(s2) after processing the strings Don't they achieve the same thing or am I missing something??
@LucidProgramming
@LucidProgramming 5 жыл бұрын
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!
@pythonenthusiast9292
@pythonenthusiast9292 4 жыл бұрын
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??
@LucidProgramming
@LucidProgramming 4 жыл бұрын
But we need to set it back to being equal to "1", right?
@marcobroca4020
@marcobroca4020 4 жыл бұрын
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")
@LucidProgramming
@LucidProgramming 4 жыл бұрын
There is no way that set has a complexity of O(1).
@brendancheong7738
@brendancheong7738 3 жыл бұрын
what is the time complexity of this algorithm? (since its faster than n log(n))
@LucidProgramming
@LucidProgramming 3 жыл бұрын
Do you have any guesses?
@brendancheong7738
@brendancheong7738 3 жыл бұрын
@@LucidProgramming Im gonna go on a wild guess and say O(n)
@LucidProgramming
@LucidProgramming 3 жыл бұрын
@@brendancheong7738 Yeah, both for time and space.
@linusjohansson3164
@linusjohansson3164 4 жыл бұрын
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?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Thanks! And yeah, MIT opencourseware has a great course on algorithms that would be right up your alley!
@linusjohansson3164
@linusjohansson3164 4 жыл бұрын
Thank you! @@LucidProgramming
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@@linusjohansson3164 Of course, and thank you for the support!
@KippiExplainsStuff
@KippiExplainsStuff 5 жыл бұрын
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.
@vincentrusso6563
@vincentrusso6563 5 жыл бұрын
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!
@KippiExplainsStuff
@KippiExplainsStuff 5 жыл бұрын
@@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.
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@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!
@shonnoronha
@shonnoronha 4 жыл бұрын
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!
@LucidProgramming
@LucidProgramming 4 жыл бұрын
The general idea is fine, but the runtime of your solution is worse.
@shonnoronha
@shonnoronha 4 жыл бұрын
@@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 !
@LucidProgramming
@LucidProgramming 4 жыл бұрын
@@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!
@larrybai
@larrybai 5 жыл бұрын
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
@LucidProgramming
@LucidProgramming 5 жыл бұрын
I think there are some edge cases for which this doesn't work.
@rizetothemoon
@rizetothemoon 5 жыл бұрын
@@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?
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@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!
@rizetothemoon
@rizetothemoon 5 жыл бұрын
@@LucidProgramming thanks for the reply. I have watched all your data structure and algo videos. Appreciate the work you've done!
@LucidProgramming
@LucidProgramming 5 жыл бұрын
@@rizetothemoon Cheers, I appreciate that, Roger!
@jillward6256
@jillward6256 Жыл бұрын
Kodie an Kyler should be ok to use as an anagr
@LucidProgramming
@LucidProgramming Жыл бұрын
You can use them to check, but those words would not be anagrams.
@mohammedrilwan9050
@mohammedrilwan9050 4 жыл бұрын
we can return false if 'i not in ht'..while iterating in S2..isnt it ?
@LucidProgramming
@LucidProgramming 4 жыл бұрын
Have you tried this with any success?
Greedy Algorithms in Python: Optimal Task Assignment
9:12
LucidProgramming
Рет қаралды 41 М.
String Processing in Python: Check Permutation
14:32
LucidProgramming
Рет қаралды 7 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 12 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,2 МЛН
Check if two strings are anagrams
9:07
CppNuts
Рет қаралды 9 М.
Binary Search - A Different Perspective | Python Algorithms
8:56
Find All Anagrams in a String - Leetcode 438 - Python
12:14
NeetCode
Рет қаралды 86 М.
String Processing in Python: Is Palindrome Permutation
11:59
LucidProgramming
Рет қаралды 7 М.
String Processing in Python: Spreadsheet Encoding
12:51
LucidProgramming
Рет қаралды 4,5 М.
String Processing in Python: Look-and-Say Sequence
11:34
LucidProgramming
Рет қаралды 19 М.
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 419 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 415 М.
Recursion for Python Beginners with Recursive Function Examples
17:54
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 23 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 12 МЛН