How Do You Find All Anagrams Of A String In Python?
The world of programming is not only diverse but also fascinating. Time and again it is proved how we can solve real time problems using algorithms and logic.
One such real time problem that we are tackling today is find all anagrams in a string problem.
So what is an Anagram? Well, a set of Anagrams can be defined as words that have totally identical characters.
These characters are identical to the point that when rearranged in a specific order, the words can be interchanged into one another.
For instance, “a gentleman” can be rearranged for “elegant man”. Hence, these two words form an Anagram.
This blog intends to teach you how you can detect Anagrams in a computer program using various approaches and algorithms.
What do you mean by an Anagram?
In our childhood we used to solve jumbled word problems where we would arrange the characters from a given word to form new words.
The concept of Anagram in computer programming is essentially the same.
Two given strings can be considered an Anagram when we rearrange the characters of one string to form the same word as the second.
For instance, let us consider the words “Race” and “Care” as two different strings.
Now, we can essentially form the word “Care” using individual characters from “R A C E”. Hence, these two words would be considered an Anagram.
Now, the algorithm for finding all anagrams in a string can be represented as follows:
Firstly, let’s start by considering “Race” as string 1 i.e str1 and “Care” as string 2 i.e str2.
- Now, in order to check whether the two strings str1 and str2 are anagrams or not, we will start with converting the words into lowercase in the two strings.
This is because the Java programming language is case sensitive and the characters “R” and “r” will not be considered the same.
- Next up, we convert the strings using str.toCharArray() function.
- Afterwards, we will be sorting the arrays using the Arrays.sort() class.
- Finally, we will check whether the two arrays are equal in characters or not by using the function- Arrays.equal()
If these sorted arrays are found to be of equal length, then you can be rest assured that they are anagrams.
Side note: The concept of Java basically compares characters of a string or an array using the ASCII value. Which is why, it would not read the characters “R” and “r” as the same.
Hence, we need to sort the arrays first within the program in order to find the solution to this type of string problems.
With that said, If the concept of find all anagrams in a string has fascinated you, you can check out the next section where we discuss different approaches for finding the potential anagrams in a given set of strings.
What approaches can be implemented for finding Anagrams in two given Strings in Python?
First check out the problem statement based on finding all anagrams in a string:
Problem Statement:
You have been given two strings. Your task is to figure out whether the strings are anagrams or not.
For Instance:
Input: str1 = “silent” str2 = “listen”
Output: “Anagram”
Now, we shall discuss the different approaches for analysing the two strings in a program.
Method 1: Using the basic Sort function
The basic sort function is used for solving a number of string problems such as reverse first k elements of queue.
The idea is to sort the given strings and compare their characters individually. If the characters are found to be equal then we can confirm that the given strings are anagrams.
The sort function is represented as follows:
Sort()
Here’s how the algorithm for the sort function will be represented:
- Start by sorting both the strings.
- Next up, we will be comparing their values. If the characters are found to be identical, we can return true.
- Otherwise, we can return false in the program.
Time Complexity for this approach:
O(N*logN)
Method 2: By Counting the Frequency of both strings
For this idea in particular, we are assuming that the set of all the characters in both the given strings is essentially small. Meaning, that all of the characters have been stored in 8 but hence, there can only be 256 characters possible.
In this approach we will be calculating the frequency of all the characters in both strings and if the frequency of both the strings match, then we can confirm that they are certainly anagrams.
Initiate the following algorithms for this approach:
- Firstly we are required to create a count array of the size 256 for both the strings.
- Next up, you can initialise the size and values of these arrays as 0.
- Now, you can start iterating through the characters of both strings while adding the counted characters in its corresponding arrays.
- Finally, you can compare both the count arrays and if they are found to be the same, you can return the statement as true or else false.
Time Complexity for this approach:
O(n)
Method 3: Implementing the One Count Array Approach
Below is the algorithm for the one count array approach:
- The approach discussed in method 2 can be optimised by using one one array instead of two.
- Start with incrementing the values of str1 and decrementing the values of str2.
- Lastly, if found that all the values of both the strings count to 0, you may conclude that both the strings are anagrams for each other.
Time Complexity for this approach:
O(n)
Wrapping Up
Did you know?
The average annual salary of a Python developer in India ranges between 1.9 to 9.3 Lakhs.
From TCS to Infosys, the Java and Python developers will always have a chance of cracking their dream role in these tech companies.
An ideal Python developer needs to be affluent in solving all sorts of data structure problems such as reverse first k elements of queue.
So, the question arises is it difficult to learn Python?
Well, if you ask the experts on our website, Python and Java are two of the most interesting and fun programming languages to learn.