Saturday, December 28, 2024
Google search engine
HomeData Modelling & AICheck whether two strings are anagrams of each other using unordered_map in...

Check whether two strings are anagrams of each other using unordered_map in C++

Write a function to check whether two given strings are an Anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different.

For example, “abcd” and “dabc” are an Anagram of each other.

check-whether-two-strings-are-anagram-of-each-other

Approach: Unordered Map can also be used to find if any two given strings are anagrams or not. The idea is to store each character of the first string in the map, with its frequency as value, and after that check for each character of the second string in the map, and if the character is found in the map, reduce its frequency value from the map. If the frequency of a character becomes 0, erase it from the map and at last if the map becomes empty, that means all the characters of the first string are there in the second string with the same number of occurrences(frequency of each character). Implementation: 

CPP




// C++ program to check whether
// two strings are anagrams of
// each other or not, using Hashmap
 
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
 
// Function to check whether two strings
// are an anagram of each other
bool isanagram(string s1, string s2)
{
    int l1 = s1.length();
    int l2 = s2.length();
 
    unordered_map<char, int> m;
    if (l1 != l2) {
        return false;
    }
    for (int i = 0; i < l1; i++) {
        m[s1[i]]++;
    }
 
    for (int i = 0; i < l2; i++) {
        if (m.find(s2[i]) == m.end()) {
            return false;
        }
        else {
            m[s2[i]]--;
            if (m[s2[i]] == 0) {
                m.erase(s2[i]);
            }
        }
    }
    return m.size() == 0;
}
 
// Test function
void test(string str1, string str2)
{
 
    cout << "Strings to be checked:\n"
        << str1 << "\n"
        << str2 << "\n";
 
    if (isanagram(str1, str2)) {
        cout << "The two strings are"
            << "anagram of each other\n";
    }
    else {
        cout << "The two strings are not"
            << " anagram of each other\n";
    }
    cout << endl;
}
 
// Driver program
int main()
{
    // Get the Strings
    string str1 = "neveropen";
    string str2 = "forneveropenneveropen";
 
    // Test the Strings
    test(str1, str2);
 
    // Get the Strings
    str1 = "neveropen";
    str2 = "neveropen";
 
    // Test the Strings
    test(str1, str2);
    return 0;
}


Output:

Strings to be checked:
neveropen
forneveropenneveropen
The two strings areanagram of each other

Strings to be checked:
neveropen
neveropen
The two strings are not anagram of each other

Time Complexity: O(l1 + l2) where l1 and l2 are lengths of strings.

Auxiliary space: O(m1 + m2) where m1 and m2 are numbers of unique characters in each string.

Please suggest if someone has a better solution which is more efficient in terms of space and time.
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Related articles:

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments