Program to check : Check if Two String are Anagram or Not
Definition & Explanation
An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. For example, “listen” and “silent” are anagrams because they contain the same letters rearranged. To check if two strings are anagrams or not, we need to compare the frequency of characters in both strings.
More Description
Program Logic
- Input Strings: Accept two strings as input.
- Count Character Frequency: Count the frequency of each character in both strings. This can be done by using an array or dictionary to store the count of each character in each string.
- Compare Counts: Compare the counts of characters in both strings. If the counts are equal for all characters, the strings are anagrams; otherwise, they are not.
- Output: Print whether the strings are anagrams or not.
C
Method 1 :
#include <stdio.h>
#include <string.h>
#define MAX_CHARS 256
// Function to check if two strings are anagrams
int areAnagrams(char *str1, char *str2) {
int count1[MAX_CHARS] = {0};
int count2[MAX_CHARS] = {0};
int i;
// Count frequency of characters in str1
for (i = 0; str1[i] && str2[i]; i++) {
count1[str1[i]]++;
count2[str2[i]]++;
}
// If lengths of strings are not equal, they can't be anagrams
if (strlen(str1) != strlen(str2))
return 0;
// Compare count arrays
for (i = 0; i < MAX_CHARS; i++)
if (count1[i] != count2[i])
return 0;
return 1;
}
int main() {
char str1[] = "listen";
char str2[] = "silent";
if (areAnagrams(str1, str2))
printf("Strings are anagrams\n");
else
printf("Strings are not anagrams\n");
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
// Function to check if two strings are anagrams
bool areAnagrams(string str1, string str2) {
unordered_map<char, int> count1, count2;
// Count frequency of characters in str1
for (char ch : str1)
count1[ch]++;
// Count frequency of characters in str2
for (char ch : str2)
count2[ch]++;
// Compare counts
return count1 == count2;
}
int main() {
string str1 = "listen";
string str2 = "silent";
if (areAnagrams(str1, str2))
cout << "Strings are anagrams" << endl;
else
cout << "Strings are not anagrams" << endl;
return 0;
}
Output :
JAVA
Method 1 :
import java.util.HashMap;
import java.util.Map;
public class AnagramCheck {
// Function to check if two strings are anagrams
public static boolean areAnagrams(String str1, String str2) {
if (str1.length() != str2.length())
return false;
Map<Character, Integer> count1 = new HashMap<>();
Map<Character, Integer> count2 = new HashMap<>();
// Count frequency of characters in str1
for (char ch : str1.toCharArray())
count1.put(ch, count1.getOrDefault(ch, 0) + 1);
// Count frequency of characters in str2
for (char ch : str2.toCharArray())
count2.put(ch, count2.getOrDefault(ch, 0) + 1);
// Compare counts
return count1.equals(count2);
}
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
if (areAnagrams(str1, str2))
System.out.println("Strings are anagrams");
else
System.out.println("Strings are not anagrams");
}
}
Output :
Python
Method 1 :
from collections import Counter
# Function to check if two strings are anagrams
def areAnagrams(str1, str2):
return Counter(str1) == Counter(str2)
# Test the function
str1 = "listen"
str2 = "silent"
if areAnagrams(str1, str2):
print("Strings are anagrams")
else:
print("Strings are not anagrams")
Output :
Strings are anagrams