Two strings are called anagrams if they contain the same characters in the same frequency, but their order is different.
Example:
Input:
Str1 = "abcd"
Str2 ="dabc"
Output:
Strings are Anagram
Different Methods to Check Whether a String Is an Anagram or Not:
In this article, we will learn how to check whether two strings are anagrams of each other in C++ using different efficient approaches.
1. Use Sorting:
Steps:
- Sort both strings
- Compare the sorted strings
#include <bits/stdc++.h>
using namespace std;
bool areAnagram(string str1, string str2){
int n1 = str1.length();
int n2 = str2.length();
if (n1 != n2)
return false;
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
int main(){
string str1 = "test";
string str2 = "ttew";
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;
}
Output
The two strings are not anagram of each other
Explanation:
- The function checks the length of both strings and returns false if they are not equal, because anagrams must have the same number of characters.
- Both strings are sorted in alphabetical order so that their characters can be compared easily.
- A loop compares each character of the sorted strings one by one.
- If all characters match, the strings are anagrams; otherwise, they are not.
2. Count characters:
- This method assumes that characters are stored using 8 bits, so there can be 256 possible characters.
- Two count arrays of size 256 are created, one for each string, and all values are initialized to 0.
- Each character of both strings is scanned, and the corresponding index in the count array is incremented.
- Finally, both count arrays are compared; if they are exactly the same, the strings are anagrams.
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
bool areAnagram(char* str1, char* str2){
int count1[NO_OF_CHARS] = {0};
int count2[NO_OF_CHARS] = {0};
int i;
for (i = 0; str1[i] && str2[i]; i++) {
count1[str1[i]]++;
count2[str2[i]]++;
}
if (str1[i] || str2[i])
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count1[i] != count2[i])
return false;
return true;
}
int main(){
char str1[] = "geeksforgeeks";
char str2[] = "forgeeksgeeks";
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;
}
Output
The two strings are anagram of each other
3. Count characters using one array:
- This optimized method uses only one count array instead of two.
- For each character in str1, the corresponding count is incremented, and for each character in str2, the corresponding count is decremented.
- After processing both strings, if all values in the count array are 0, then the two strings are anagrams of each other.
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
bool areAnagram(char* str1, char* str2){
int count[NO_OF_CHARS] = { 0 };
int i;
for (i = 0; str1[i] && str2[i]; i++) {
count[str1[i]]++;
count[str2[i]]--;
}
if (str1[i] || str2[i])
return false;
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
int main(){
char str1[] = "geeksforgeeks";
char str2[] = "forgeeksgeeks";
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;
}
Output
The two strings are anagram of each other
4. Using unordered_map:
- Instead of using a fixed-size array of 256 characters, an unordered_map is used to store only the characters that appear in the strings.
- First, the occurrences of each character in the first string are counted and stored in the unordered_map.
- Then, while scanning the second string, the count of each character is reduced.
- Finally, if the count of every character in the unordered_map is 0, the two strings are anagrams; otherwise, they are not.
#include <bits/stdc++.h>
using namespace std;
bool isAnagram(string a,string b){
if (a.length() != b.length()) {
return false;
}
unordered_map<char,int> m;
for (int i = 0; i < a.length(); i++) {
m[a[i]]++;
}
for (int i = 0; i < b.length(); i++) {
if (m[b[i]]) {
m[b[i]] -= 1;
}
}
for (auto items : m) {
if (items.second != 0) {
return false;
}
}
return true;
}
int main(){
string str1 = "geeksforgeeks";
string str2 = "forgeeksgeeks";
if (isAnagram(str1, str2))
cout<<"The two strings are anagram of each other"<<endl;
else
cout<<"The two strings are not anagram of each other"<<endl;
}
Output
The two strings are anagram of each other