# Check if two strings are anagrams or not in C, C++, Java and Python | faceprep

Program to check if two strings are anagrams or not is discussed here. Two strings are given as input and those strings have to be checked if they are anagrams or not.

Anagram means that both strings contain the same character set, only their order is different. Therefore, in both strings, the frequency of each letter must be the same. For example, strings “act” and “cat” are anagrams.

The solution to this problem can be given in two different ways.

Method 1: Count the frequency of alphabets in both the strings and store them in respective arrays. If the two arrays are equal, return true. Else, return false.

Method 2: Sort both the strings and compare if both the sorted strings are equal. If they are equal, return true. Else, return false

## Algorithm to check if two strings are anagrams or not

• Input the two strings.
• Create an array for both the strings.
• Traverse both the strings and store the count of the alphabets of both the strings in respective arrays.
• Check if both the arrays are equal.
• If both the arrays are equal, return true. Else, return false.

## Program to check if two strings are anagrams or not​

// C program to check if the strings are anagrams or not

#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
char a[100], b[100];

printf(“Enter two strings : \n”);
gets(a);
gets(b);

if (check_anagram(a, b) == 1)
printf(“The strings are anagrams\n”);
else
printf(“The strings are not anagrams\n”);

return 0;
}

int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c=0;

// Calculating frequency of characters of first string

while (a[c] != ‘\0’)
{
first[a[c]-‘a’]++;
c++;
}

c = 0;

while (b[c] != ‘\0’)
{
second[b[c]-‘a’]++;
c++;
}

// Comparing frequency of characters

for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}

return 1;
}

// C++ program to check if the two strings are anagrams or not

#include <bits/stdc++.h>
using namespace std;
int check_anagram(string s1, string s2);
int main()
{
int n;
string s1,s2;
cout <<“\nEnter two strings : “;
cin>>s1;
cin>>s2;
if(check_anagram(s1,s2))
printf(“\nYes\n”);
else
printf(“\nNo\n”);
return 0;
}

int check_anagram(string s1,string s2)
{
int a1[26]={0}, a2[26]={0};

//if string lengths are different then they are not anagrams
if(s1.length()!=s2.length())
return 0;

// count the frequency of char in both strings

//for string1 – storing frequency for each letter in the string
for(int i=0; s1[i]!=’\0′; i++)
{
a1[s1[i]-‘a’]++;
}

//storing frequency for each letter in the string
for(int i=0; s2[i]!=’\0′; i++)
{
a2[s2[i]-‘a’]++;
}

//Anagram check – comparison step the frequencies of each char in both strings
for(int i=0; i<26; i++)
{
if(a1[i] != a2[i])
return 0;
}
return 1;
}

// Java program to check if the strings are anagrams or not

import java.util.Scanner;

public class Main
{
public static void main(String[] input)
{
String str1, str2;
int len, len1, len2, i, j, found=0, not_found=0;
Scanner scan = new Scanner(System.in);

System.out.print(“Enter First String : “);
str1 = scan.nextLine();
System.out.print(“Enter Second String : “);
str2 = scan.nextLine();

len1 = str1.length();
len2 = str2.length();

if(len1 == len2)
{
len = len1;
for(i=0; i<len; i++)
{
found = 0;
for(j=0; j<len; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found = 1;
break;
}
}
if(found == 0)
{
not_found = 1;
break;
}
}
if(not_found == 1)
{
System.out.print(“Strings are not Anagrams”);
}
else
{
System.out.print(“Strings are Anagrams”);
}
}
else
{
System.out.print(“Both Strings Must have the same number of Character to be an Anagram”);
}
}
}

Time complexity: O(n)

## Algorithm to check if two strings are anagrams or not using sorting technique

• Input the strings.
• Sort both the strings.
• If both the strings are equal, return true. Else, return false.
Program to check if the strings are anagrams or not using the sorting technique is given below.

// C++ program to check if the strings are anagrams

#include <bits/stdc++.h>
using namespace std;

// function to check whether two strings are anagram of each other
bool areAnagram(string str1, string str2)
{
// Get lengths of both strings
int n1 = str1.length();
int n2 = str2.length();

// If length of both strings is not same, then they
// cannot be anagram
if (n1 != n2)
return false;

// Sort both the strings
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());

// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;

return true;
}

int main()
{
string str1;
string str2;
cout << “\nInput the strings : “;
cin >> str1 >> str2;
if (areAnagram(str1, str2))
cout << “The two strings are anagram of each other”;
else
cout << “The two strings are not anagram of each other”;

return 0;
}

// Java program to check whether two strings are anagrams of each other

import java.io.*;
import java.util.Arrays;
import java.util.Collections;

class Main{

// function to check whether two strings are anagrams
static boolean areAnagram(char[] str1, char[] str2)
{
int n1 = str1.length;
int n2 = str2.length;

// If length of both strings is not same,
// then they cannot be anagrams
if (n1 != n2)
return false;

// Sort both strings
Arrays.sort(str1);
Arrays.sort(str2);

// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;

return true;
}

public static void main(String args[])
{
char str1[] = { ‘c’,’a’,’t’ };
char str2[] = { ‘a’,’c’,’t’ };
if (areAnagram(str1, str2))
System.out.println(“The two strings are”
+ ” anagram of each other”);
else
System.out.println(“The two strings are not”
+ ” anagram of each other”);
}
}

# Python program to check if the strings are anagrams or not

str1 = input(“Enter first string : “)
str2 = input(“Enter second string : “)
if(sorted(str1) == sorted(str2)):
print(“Strings are anagrams”)
else:
print(“Strings are not anagrams”)

Output: