Programa C++ para verificar si dos strings son anagramas entre sí

Escriba una función para verificar si dos strings dadas son anagramas entre sí o no. Un anagrama de una string es otra string que contiene los mismos caracteres, solo el orden de los caracteres puede ser diferente. Por ejemplo, «abcd» y «dabc» son un anagrama el uno del otro.

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

C++

// 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";
 
    // Function Call
    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;
}

C++

// C++ program to check if two strings
// are anagrams of each other
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
 
/* Function to check whether two
   strings are anagram of each other */
bool areAnagram(char* str1, char* str2)
{
    // Create 2 count arrays and initialize
    // all values as 0
    int count1[NO_OF_CHARS] = {0};
    int count2[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++)
    {
        count1[str1[i]]++;
        count2[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;
 
    // Compare count arrays
    for (i = 0; i < NO_OF_CHARS; i++)
        if (count1[i] != count2[i])
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    char str1[] = "geeksforgeeks";
    char str2[] = "forgeeksgeeks";
   
    // Function Call
    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;
}
// This is code is contributed by rathbhupendra

C++

// C++ program to check if 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)
{
    // 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;
}
 
// Driver code
int main()
{
    char str1[] = "geeksforgeeks";
    char str2[] = "forgeeksgeeks";
   
    // Function call
    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;
}

C++

// C++ program to check if two
// strings are anagrams of each other
#include <bits/stdc++.h>
using namespace std;
 
bool isAnagram(string a,string b)
{
// Check if both strings has same length or not
if (a.length() != b.length()) {
    return false;
}
 
// Initialising unordered_map
unordered_map<char,int> m;
 
// Storing the count of each character
// present in first String
for (int i = 0; i < a.length(); i++) {
    m[a[i]]++;
}
 
// Now iterating over second String
for (int i = 0; i < b.length(); i++) {
    // Check if unordered_map already contain the current
    // character or not
    if (m[b[i]]) {
    // If contains reduce count of that
    // character by 1 to indicate that current
    // character has been already counted as
    // idea here is to check if in last count of
    // all characters in last is zero which
    // means all characters in String a are
    // present in String b.
    m[b[i]] -= 1;
    }
}
 
// Loop over all keys and check if all keys are 0
// as it means that all the characters are present
// in equal count in both strings.
for (auto items : m) {
    if (items.second != 0) {
    return false;
    }
}
// Returning True as all keys are zero
return true;
}
 
// Driver code
int main()
{
string str1 = "geeksforgeeks";
string str2 = "forgeeksgeeks";
 
// Function call
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;
}
 
// This code is contributed by Pushpesh Raj

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *