[07 Feb 2020] Check whether two strings are anagram of each other





Check whether two strings are anagram of each other


Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.check-whether-two-strings-are-anagram-of-each-other
// C++ program to check whether two strings are anagrams
// of each other
#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;
}
  
// Driver code
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;
}
bool areAnagram(char* str1, char* str2)
{
    // Create a count array and initialize all values as 0
    int count[NO_OF_CHARS] = { 0 };
    int i;
  
    // For each character in input strings, increment count in
    // the corresponding count array
    for (i = 0; str1[i] && str2[i]; i++) {
        count[str1[i]]++;
        count[str2[i]]--;
    }
  
    // If both strings are of different length. Removing this condition
    // will make the program fail for strings like "aaca" and "aca"
    if (str1[i] || str2[i])
        return false;
  
    // See if there is any non-zero value in count array
    for (i = 0; i < NO_OF_CHARS; i++)
        if (count[i])
            return false;
    return true;
}
Time Complexity: O(n)


# Python program to check whether two strings are 
# anagrams of each other 
  
# function to check whether two strings are anagram 
# of each other 
def areAnagram(str1, str2): 
    # Get lengths of both strings 
    n1 = len(str1) 
    n2 = len(str2) 
  
    # If lenght of both strings is not same, then 
    # they cannot be anagram 
    if n1 != n2: 
        return 0
  
    # Sort both strings 
    str1 = sorted(str1)
    str2 = sorted(str2)
  
    # Compare sorted strings 
    for i in range(0, n1): 
        if str1[i] != str2[i]: 
            return 0
  
    return 1
  
  
# Driver program to test the above function 
str1 = "test"
str2 = "ttew"
if areAnagram(str1, str2): 
    print ("The two strings are anagram of each other")
else
    print ("The two strings are not anagram of each other")
  
# This code is contributed by Bhavya Jain 

























Comments

Popular posts from this blog

[16 Feb 2020] Given an array where every element occurs three times, except one element which occurs only once.

Which data structure is used in redo-undo feature?

Important Program Collection