C++ 程序检查两个字符串是否互为 Anagram
编写一个函数来检查两个给定的字符串是否是彼此的字谜。一个字符串的变位词是另一个包含相同字符的字符串,只是字符的顺序可以不同。例如,“abcd”和“dabc”是彼此的字谜。
我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。
方法一(使用排序):
- 对两个字符串进行排序
- 比较排序后的字符串
下面是上述思想的实现:
C++
// C++ program to check whether two
// strings are anagrams of each other
#include
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
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
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;
}
输出:
The two strings are not anagram of each other
时间复杂度: O(nLogn)
方法2(计数字符):
此方法假定两个字符串中可能的字符集都很小。在下面的实现中,假设字符使用 8 位存储,可能有 256 个字符。
- 为两个字符串创建大小为 256 的计数数组。将计数数组中的所有值初始化为 0。
- 遍历两个字符串的每个字符并增加相应计数数组中的字符计数。
- 比较计数数组。如果两个计数数组相同,则返回 true。
下面是上述思想的实现:
C++
// C++ program to check if two strings
// are anagrams of each other
#include
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
输出:
The two strings are anagram of each other
方法3(使用一个数组计算字符):
上述实现可以进一步只使用一个计数数组而不是两个。我们可以为 str1 中的字符递增 count 数组中的值,为 str2 中的字符递减。最后,如果所有计数值都是 0,那么这两个字符串是彼此的字谜。感谢Ace提出此优化建议。
C++
// C++ program to check if two strings
// are anagrams of each other
#include
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;
}
输出:
The two strings are anagram of each other
时间复杂度: O(n)
有关详细信息,请参阅关于检查两个字符串是否是彼此的字谜的完整文章!