Signup/Sign In
LAST UPDATED: MAY 11, 2023

Check if Two Strings are Anagrams of Each Other

Technology #c#algorithm#cpp

    Let's start by understanding the problem statement for this task.

    You are given two different strings, your work is to find out whether the two strings are anagrams of each other or not.

    Hint:

    Two strings are said to be anagrams of each other when both of the strings contain the same characters, and the prime case is they must be of the same size.

    Quick Think:

    For checking anagram for the two strings, we need t check for the following conditions:-

    • We need to check for the size of the two strings.
    • We need to check for the count of each character, if they all are equal, then both strings are anagrams of each other.

    Algorithm:

    For implementing the above algorithm, we need to create a function to check for both the strings with arguments as the first string and the other string and follow the following steps:-

    Step1: Initialize the two arrays with integer 0 and the size of the array should be the total number of characters, i.e., 256.

    Step2: A loop should be initialized to store the total occurrence of the characters in both the separate arrays for the two different strings.

    Step3: Now, for the first condition check for the length of the two strings if the strings are of the same length then go to step 4 else return false or 0 (in case if the return type is of the function is an integer).

    Step4: Now, check for the occurrence of each and every character in the two strings by iterating through a loop if the occurrence of the characters in the two strings is not equal then return false or 0 (in case when the return type of the function is an integer).

    Implementation Of The Above Algorithm:

    Now let's see the implementation of the above algorithm,

    #include <bits/stdc++.h>
    using namespace std;
    
    /*Function to check the two strings. */
    bool anagrams(char *str_1, char *str_2)
    {
    	int arr_1[256] = { 0 };
    
    	int arr_2[256] = { 0 };
    
    	int i;
    	for (i = 0; str_1[i] && str_2[i]; i++)
    	{
    		/*First array to store the frequency of the first string. */
    		arr_1[str_1[i]]++;
    
    		/*second array to store the frequency of the second string. */
    		arr_2[str_2[i]]++;
    	}
    
    	/*If length is not equal return false. */
    	if (arr_1[i] || arr_2[i])
    		return false;
    
    	for (i = 0; i < 256; i++)
    		if (arr_1[i] != arr_2[i])
    			return false;
    
    	return true;
    }
    
    /*Driver program to test the above functions*/
    int main()
    {
    	char str_1[] = "TRIANGLE";
    	char str_2[] = "INTEGRAL";
    	if (anagrams(str_1, str_2))
    		cout << "The two strings are an anagram of each other" << endl;
    	else
    		cout << "The two strings are not an anagram of each other" << endl;
    	return 0;
    
    }

    Running Time Complexity Of The Above Algorithm Is:- O(n)

    The Output Of The Above Algorithm Is:- The two strings are an anagram of each other.

    Explanation Of The Above Algorithm:

    For understanding the above algorithm, let us take an example of the above algorithm as shown in the diagram above and follow the following steps to reach the output:-

    • The word “TRIANGLE” and the word “INTEGRAL” are anagrams of each other, let us check how.

    An Algorithm to Check if Two Strings are Anagrams of Each Other

    • The occurrences of letters of the first string “T”, “R”, “I”, “A”, “N”, “G”, “L” and “E” is stored in the first array which was initially initialized as 0 as shown in the diagram above.

    An Algorithm to Check if Two Strings are Anagrams of Each Other

    • Similarly, the occurrences of the letter of the second string “I”, “N”, “T”, “E”, “G”, “R”, “A”, “L” are stored in the second array which was initially initialized as 0 as shown in the diagram above.
    • Now, we see that the length of both the strings are equal so we now, check for the count of the different characters in the two strings, and we see that the count is the same as shown in the above diagram.
    • Hence, the two strings are anagrams of each other.
    I best describe myself as a tech enthusiast with a good knowledge of data structures and algorithms and programming languages such as c programming, c++ programming and a beginner in python 3 with also knowledge discrete mathematics.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS