Problem: Write a program in python to check whether the two given numbers are Anagram or not.
What is an Anagram?
An anagram is any word that can be used to form another word by rearranging the letters of the original word.
Examples:
- Tar <=> Rat
- Arc <=> Car
- Elbow <=> Below
- State <=> Taste
- Cider <=> Cried
- Dusty <=> Study
Check Anagram in Python
In Python, we can easily check anagram words by using the if
clause and sorted()
function. Let’s see an example for a better understanding.
w1="act"
w2="cat"
if (sorted(w1) == sorted(w2)):
print('Words are Anagram')
else:
print('Words are not Anagram')
Output:
Words are Anagram
Explanation:
The two words can only be anagrams if they are meaningful and are compromised of the same letters.
So to check, we accept two words as inputs and sort them in alphabetical order with the help of the built-in sorted()
method.
After sorting the words, we compare them using the ==
operator to know whether they are the same or not. If yes then the word combination is an anagram otherwise not.
E.g
Input: act , cat
Sorted: act, act
Both are the same/equal so they are an anagram.