C++ Program to Check Anagram with Example
std::sort and compares them with == to return true if they match, false otherwise.Examples
How to Think About It
Algorithm
Code
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
bool areAnagrams(std::string s1, std::string s2) {
s1.erase(std::remove_if(s1.begin(), s1.end(), ::isspace), s1.end());
s2.erase(std::remove_if(s2.begin(), s2.end(), ::isspace), s2.end());
std::transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
std::sort(s1.begin(), s1.end());
std::sort(s2.begin(), s2.end());
return s1 == s2;
}
int main() {
std::string str1, str2;
std::cout << "Enter first string: ";
std::getline(std::cin, str1);
std::cout << "Enter second string: ";
std::getline(std::cin, str2);
if (areAnagrams(str1, str2))
std::cout << "Anagram" << std::endl;
else
std::cout << "Not an Anagram" << std::endl;
return 0;
}Dry Run
Let's trace the example input "listen" and "silent" through the code
Input strings
str1 = "listen", str2 = "silent"
Remove spaces
No spaces to remove, strings remain "listen" and "silent"
Convert to lowercase
Both strings already lowercase: "listen", "silent"
Sort strings
Sorted str1 = "eilnst", Sorted str2 = "eilnst"
Compare sorted strings
"eilnst" == "eilnst" is true
Print result
Output: Anagram
| Step | str1 | str2 |
|---|---|---|
| Initial | listen | silent |
| After removing spaces | listen | silent |
| After lowercase | listen | silent |
| After sorting | eilnst | eilnst |
Why This Works
Step 1: Normalize strings
Removing spaces and converting to lowercase ensures the comparison ignores case and spaces, making it fair.
Step 2: Sort characters
Sorting rearranges letters so that if two strings have the same letters, their sorted forms will be identical.
Step 3: Compare sorted strings
If sorted strings match exactly, the original strings are anagrams; otherwise, they are not.
Alternative Approaches
#include <iostream>
#include <string>
#include <cctype>
bool areAnagrams(std::string s1, std::string s2) {
int count[256] = {0};
for (char c : s1) {
if (!isspace(c)) count[tolower(c)]++;
}
for (char c : s2) {
if (!isspace(c)) count[tolower(c)]--;
}
for (int i = 0; i < 256; i++) {
if (count[i] != 0) return false;
}
return true;
}
int main() {
std::string str1, str2;
std::cout << "Enter first string: ";
std::getline(std::cin, str1);
std::cout << "Enter second string: ";
std::getline(std::cin, str2);
if (areAnagrams(str1, str2))
std::cout << "Anagram" << std::endl;
else
std::cout << "Not an Anagram" << std::endl;
return 0;
}#include <iostream>
#include <string>
#include <unordered_map>
#include <cctype>
bool areAnagrams(std::string s1, std::string s2) {
std::unordered_map<char, int> freq;
for (char c : s1) {
if (!isspace(c)) freq[tolower(c)]++;
}
for (char c : s2) {
if (!isspace(c)) freq[tolower(c)]--;
}
for (auto &p : freq) {
if (p.second != 0) return false;
}
return true;
}
int main() {
std::string str1, str2;
std::cout << "Enter first string: ";
std::getline(std::cin, str1);
std::cout << "Enter second string: ";
std::getline(std::cin, str2);
if (areAnagrams(str1, str2))
std::cout << "Anagram" << std::endl;
else
std::cout << "Not an Anagram" << std::endl;
return 0;
}Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting both strings takes O(n log n) time, where n is the length of the strings. Comparison is O(n).
Space Complexity
Extra space is used for sorting and storing copies of the strings, which is O(n).
Which Approach is Fastest?
Frequency counting methods run in O(n) time and can be faster than sorting for large inputs, but sorting is simpler to implement.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Sorting and comparing | O(n log n) | O(n) | Simple and short strings |
| Frequency counting with array | O(n) | O(1) | ASCII characters, large strings |
| Frequency counting with unordered_map | O(n) | O(n) | Unicode or extended character sets |