Updated March 24, 2023
Introduction to Anagram Program in Python
An anagram is a situation where among the two given strings or numbers, one of them is a rearranged form of another string or number which is being used, so this means every character in one string or number will be a part of the other string, which in place so in that case, though both the strings mean a different subject they can be rearranged and unified.
Example:
HEART
EARTH
In the above example, the word heart can be formed from the word earth, or the word earth can also be formulated as a heart.
Examples of Anagram Program in Python
Below are the techniques and examples of the Anagram Program in Python:
1. Counter Technique
The counter technique involves the calculation of the count of each word in both the given strings. If each word’s count in a given string matches the count of every word in another string, then the corresponding word set is declared as an Anagram.
Code:
from collections import Counter, defaultdict
def anagram_check(keywords):
anagrams = defaultdict(list)
for i in keywords:
histogram = tuple(Counter(i).items())
anagrams[histogram].append(i)
return list(anagrams.values())
keywords = ("hello","helol")
print(anagram_check(keywords))
Output:
Explanation:
In this program, the words to be checked for anagram is keyed in as string values. for every disposition or a character in a piece of the words which are keyed in their equivalent count is recorded using the counter function. The determined count is appended in a list and kept on track. Once this process of count calculation is accomplished for all the characters in the first string, then the same process is accomplished for characters in the text string. If the count of characters in both strings is matched, both the strings are considered an Anagrams.
2. Sort Technique
In the sort technique, two different strings are sorted and verified whether both values generated for both strings after the sorting process is a perfect match for each other.
Code:
def Anogram_check(string1, string2):
# Strings are sorted and verified
if(sorted(string1)== sorted(string2)):
print("Both strings form a Anogram.")
else:
print("Both strings do not form as a Anogram.")
# driver code
string1 ="EARTH"
string2 ="HEART"
print( "String value1 : ", string1 )
print( "String value2 : ", string2 )
Anogram_check(string1, string2)
Output:
Explanation:
Sorting is the most simple technique for achieving Anagram checks; Here, two different strings are passed as arguments to the Anagram check function. In the anagram function, both the strings are sorted in a specific order. So once both the strings get sorted, then they are compared against each other. If the comparison between the strings matches, then the given strings are declared as Anagram; else, they are notified as not anagrams. As mentioned before, this technique for determining anagrams are effective and simple. It reduces code complexity to a very great extent.
3. Positional Verification Technique
In this technique, a positional level comparison for an anagram is carried out. Here anagram check is achieved by means of verifying the positional character in one string with every positional character in the other string. If assessment got hold of from every position of one string holds an equivalent match in the other string, it is declared an anagram.
Code:
def anagram_check(string1,string2):
check_variable = True
if len(string1) != len(string2):
check_variable = False
alist = list(string2)
position_string1 = 0
while position_string1 < len(string1) and check_variable:
position_string2 = 0
found = False
while position_string2 < len(alist) and not found:
if string1[position_string1] == alist[position_string2]:
found = True
else:
position_string2 = position_string2 + 1
if found:
alist[position_string2] = None
else:
check_variable = False
position_string1 = position_string1 + 1
return check_variable
string1 = "abc"
string2 = "cba"
print("String value1 : " , string1)
print("String value2 : " , string2)
Boolean_out = anagram_check('abcd','dcba')
if Boolean_out:
print( "Both words are Alogram " )
else:
print( "Both words are not Alogram " )
Output:
Explanation:
This is another effective technique for verifying anagrams between two given string values; here again, two different string values are considered for comparison. According to the string values, which have been passed on into two loops for the verification process, these loops are set in a nested manner for execution. One among the loop is being used to process one of the string, and the other loop is used for the other string used. A character from the primary string is chosen and established next to each and every character present in the second string, and this process is carried on for every letter in the first string. Here every character which is picked is verified for its presence in the second string. If all the first string characters hold a position in the second string, then both the words are considered an anagram. In case any of the characters does not pull a comparative match in the second string, then both the strings are considered to be as not anagrams. Verifying anagrams by this technique is a very stable process because at any point, irrespective of the complexities of the words involved, the anagram factor can be very precisely judged in means of using this algorithmic strategy.
4. Reverse Anagram Check
Here an anagram is achieved in such a manner.
Code:
words = ["race", "ecar", "meet", "em"]
anagrams = {}
for word in words:
reverse_word=word[::-1]
if reverse_word in words:
anagrams[word] = (words.pop(words.index(reverse_word)))
print(anagrams)
Output:
Explanation:
This is a very rarely used technique; this is used for comparing anagrams among the reversed strings. Here again, two different strings are keyed in. As very similar to palindromes here, one among the strings is reversed and verified with the other string. Both strings are matched; if a match is formed, then the strings are declared as anagrams; if no match is formed, they are notified as not anagrams.
Conclusion
In the coding area, the process of anagram check between words and integers can be achieved by several means and some of the predominant techniques are exposed above with simple and flexible coding standards.
Recommended Articles
This is a guide to Anagram Program in Python. Here we discuss the Techniques of the Anagram Program in Python and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –