Hey all, in this amazing tutorial, we are going to check if two strings are Anagram or not. So gear up as we are going to do this program in the best way possible!
As always, you could find the final code at the end of this tutorial. But I would rather suggest you to stick with us this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world. Excited? Let's start!
What is an Anagram?
In simple language, if two strings have same characters irrespective of the order in which they occur, then they are said to be anagram of each other.
For example: Heart- Earth
Here, both Heart and Earth contain same characters irrespective of the order in which they occur. Hence, this is an anagram!
One important point to note is that the length of the strings should be equal (i.e., the frequency of a character in both the strings must be same).
Other examples of anagrams are:
- Dormitory - Dirty room
- Conversation - Voices rant on
- The eyes - They see
- A gentleman - Elegant man
* We have to ignore the spaces while checking for anagrams.*
I hope that you are now able to understand what an anagram really is. Now let's start with the overview!
Overview:
In this problem, we are asked to check if two strings are anagram or not. The only input required from the user would the two strings.
We would be using simple function called sorted(), which would sort both the strings in alphabetical order. Then we would just check if both the sorted strings are same or not. If they are, then both the strings are anagram of one other.
This the most basic part of checking anagrams. But when we look deep into the topic, we also find that anagrams aren't necessarily of one word only, but can be of multiple words. So, taking this into account, we need to remove all the spaces from the strings (if they have them). This would be done using the replace() function.
*Otherwise the program would read the 'space' as another character and give incorrect results.*
After this, we would also need to ensure that all letters in both the strings should be of same case. If we don't do it, then we won't be able to correctly run the program if the user entered any capital word. We need to do this on our own. So we would use the lower() built-in method to ensure that the case of both the strings is lower.
Another check that would help us in easily identifying anagram is the length test. Two strings are anagrams, then this means that both of them are of same length. (That's another reason as to why we removed all the spaces in the first place!)
*As I have already told the basics of the program, you could try to write the program on your own. Check if it matches ours!*
Understanding what we need to do:
Here, I will tell you step by step what you need to do.
Step 1: Asking the user for the input:
This is the easiest part as here, we would be asking the user to enter both the strings. Let the variables be called, 'str1' and 'str2'.
Here's the code for this step:
Step2: Removing all the spaces from the strings:
As we already discussed in the Overview section, we need to remove spaces from the strings (if they have them) so that the program can work correctly.
Otherwise, our program won't be able to work correctly with inputs like: Dormitory - Dirty room. As here, our program would think the 'space' as another character and hence won't be able to output the correct response.
So to eliminate such instances, we need to remove all the spaces. We can do this using the replace() function.
Here, we will replace all " " with "".
Understood the difference? In the first one, there is one empty space that denotes 'space' while the next one has nothing. It's void.
You can know more about the replace() function here:
Here's the code for this step:
Step 3: Ensuring the case is same
In the Overview, we talked about the problem our program would face if the user enters any capital letter by mistake. Hence, after removing all the spaces, our next task would be to homogenize the case of all the characters in the two strings.
We will be doing this using the lower() method for strings. This will make the case of all the characters in the strings as lowercase. You could also choose upper(), that doesn't make any difference.
Here's the code for this step:
Step 4: Checking if the two strings are anagram or not:
Now comes the main part of the program. As we have already done everything we could to eliminate the errors by user, we can now start checking if the strings are anagram or not.
An efficient way to eliminate those strings which are not anagram is by checking their lengths. Although this may not apply in every case, but it would act as a preliminary filter.
This uses the fact that if two strings are anagrams, then they must be of same length.
We can do this by using if statement. If the lengths of two strings are equal, then they may or may not be anagram. But if the lengths are unequal, then they definitely aren't anagram!
Now that we have eliminated some inputs, let's turn to the real test.
To check if two strings are anagram, we could sort both the strings (in alphabetical order) and then compare them. If both the sorted strings are same, then they are anagram. If they are not, then they aren't anagram!
Here's the code for this step:
So, the chronology of the program is:
- Asking the user to input the two strings
- Removing the spaces in-between them using replace() method.
- Homogenizing the case of the characters. We used the lower() method to convert the string into lower case.
- Checking if both the strings are of same length.
- If they are, then they may or may not be anagram.
- But if they aren't, then its sure that they aren't anagram.
- Sorting the strings in alphabetical order and then comparing them.
- If the sorted strings are same, they are anagram.
- If not, then they aren't anagrams.
I hope that you understood the code, if you didn't then please try to test out the program by doing your own modifications. That is the best way to learn to code. Learning from our own failure, that's what coding is! Then also, the comment section is always open for discussion!
Here's the final code:
Hence, we have successfully found the best way to check if two strings are anagram or not!
Here's the output of the code:
This is the end of our amazing journey where we found the best way to check if two strings are anagram or not!
I hope you learnt something new today. And if you did, then don't forget to share it with others as it may help someone struggling with coding!
Also, don't forget to subscribe to our newsletter as we do these coding sessions regularly!
Again, thanks for reading!
No comments:
Post a Comment