Python Program to Find AnagramIn this tutorial, we will learn how we can detect if given strings are anagram. But first we should familiar with the anagram’s concept. What is Anagram?An Anagram is a condition where one string or number is rearranged in that manner; each character of the rearranged string or number must be a part of another string or number. In other words, a string is said to be an anagram of another if the second is a simple rearrangement of the first string. For example –The python and yphton are anagrams; Java and avaJ are anagrams as well. We will solve this problem before approaching the solution. Let’s understand the problem description. Problem Description
Program –Output: True Explanation – In the above code, we have declared the anagramCheck() method, which takes two strings as an argument. These strings are converted into a list to sort. Then, we defined the position variable and assigned it zero. In each iteration of the while loop, the string length is compared to the position value. Each element of both lists compared to each other and increased the position value by one. Once the position value became greater than the string length, the loop will be terminated, and it returns true; otherwise, it will return false. Example of Anagram Program in PythonThere are few techniques and example which we can use to find the anagram in Python. These techniques are given below. 1. Counter TechniqueIn this technique, we calculate the count of each character in both the given strings. If the count of a given string matches another string, then the corresponding word is an anagram of another word. Let’s understand the following example. Example – Output: [['python'], ['yphotn']] Explanation – We have imported the collection module and its Count and defaultdict method to check the string’s anagram in the above code. We have defined the checking_anagram() method to count and record each character using the counter function. Each count is resorted in a list and kept on track. This process is accomplished for all the characters in the first string than the second string. If the count of both strings is matched, it means both the strings are Anagrams.
In this technique, both the strings are sorted and check whether both values are the matching for each other. Let’s understand the following example. Example – Output: String value1 : python String value2 : ythopn Both strings are an Anagram. Explanation – In the above code, we have defined the checking_anagram() method and passed the two strings. In the checking_anagram() method, we stored the string in a specific variables. We compared each string after the sorting. If the comparison between strings matched, the given string form as anagram; else, they returned as Both strings are not anagrams. This method is relatively easy and effective. It cuts the code complexity to a very significant level. 2. Reverse Anagram CheckWe can apply this technique as follows. Example – Output: {'cat': 'tac'} Explanation – In the above code, we have used this technique to comparing anagrams among the reversed string. Here, we have formed two different strings. This technique is similar to palindromes, where we reversed one among the string and checked with other strings. If they are matched, the strings have formed an anagram; if they don’t match, they are not notified as not anagrams. 3. Position Verification TechniqueIn this method, a position level compares to check anagram. We can achieve this by verifying first string’s positional character with each positional character string in the other string. If first string holds a similar match with the other string, it is declared an anagram. Let’s understand the following example. Example – Output: String value1 : ythopn String value2 : python Explanation: In this, another technique to get the anagram of the two strings. Here we have also used the comparison. In the nested while loop, we pass the string into these loops for the verification process. The outer while loop is used for processing one of the strings, and the inner loop is used for the other string. One string’s character is compared to each character’s other string, and this process is continued on every letter in the first string. If all the first string characters are matched with the other string, then both strings are meant to be an anagram. This technique is a very stable process because it works on the algorithm technique, precisely judging the strings. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263549.html