如何检查两个字符串是否互为变位词

字谜是通过重新排列不同字符串的字母而形成的字符串。检查两个字符串是否是彼此的字谜可能听起来很困难,但这只是有点棘手且看似简单。在本文中,您将学习如何使用 C++、Python 和 JavaScript 检查两个字符串是否互为变位词。

问题陈述

给定两个字符串 s1 和 s2,您需要检查这两个字符串是否互为字谜。

示例 1 :让 s1 = "creative" 和 s2 = "reactive"。

由于可以通过重新排列第一个字符串的字母来形成第二个字符串,反之亦然,因此这两个字符串是彼此的字谜。

示例 2 :让 s1 =“Peter Piper 采摘一撮腌辣椒”和 s2 =“Peter Piper 采摘的一啄腌辣椒”。

由于第二个字符串不能通过重新排列第一个字符串的字母形成,反之亦然,因此这两个字符串不是彼此的字谜。

检查两个字符串是否互为异位词的过程

您可以按照以下方法检查两个字符串是否互为字谜:

  1. 比较两个字符串的长度。
  2. 如果两个字符串的长度不同,则表示它们不能互为字谜。因此,返回false。
  3. 如果两个字符串的长度相同,则继续。
  4. 对两个字符串进行排序。
  5. 比较两个排序的字符串。
  6. 如果两个排序的字符串相同,则意味着它们是彼此的字谜。因此,返回true。
  7. 如果两个排序的字符串不同,则意味着它们不是彼此的字谜。因此,返回false。

相关:如何检查字符串是否为回文

C++ 程序检查两个字符串是否互为变位词

下面是 C++ 程序来检查两个字符串是否是彼此的字谜:

 #include <bits/stdc++.h>
using namespace std;
bool checkAnagrams(string s1, string s2)
{
int size1 = s1.length();
int size2 = s2.length();
// If the length of both strings are not the same,
// it means they can't be anagrams of each other.
// Thus, return false.
if (size1 != size2)
{
return false;
}
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
for (int i = 0; i < size1; i++)
{
if (s1[i] != s2[i])
{
return false;
}
}
return true;
}
int main()
{
string s1 = "listen";
string s2 = "silent";
cout << "String 1: " << s1 << endl;
cout << "String 2: " << s2 << endl;
if(checkAnagrams(s1, s2))
{
cout << "Yes, the two strings are anagrams of each other" << endl;
}
else
{
cout << "No, the two strings are not anagrams of each other" << endl;
}
string s3 = "Welcome to MUO";
string s4 = "MUO to Welcome";
cout << "String 3: " << s3 << endl;
cout << "String 4: " << s4 << endl;
if(checkAnagrams(s3, s4))
{
cout << "Yes, the two strings are anagrams of each other" << endl;
}
else
{
cout << "No, the two strings are not anagrams of each other" << endl;
}
string s5 = "Peter Piper picked a peck of pickled peppers";
string s6 = "A peck of pickled peppers Peter Piper picked";
cout << "String 5: " << s5 << endl;
cout << "String 6: " << s6 << endl;
if(checkAnagrams(s5, s6))
{
cout << "Yes, the two strings are anagrams of each other" << endl;
}
else
{
cout << "No, the two strings are not anagrams of each other" << endl;
}
string s7 = "She sells seashells by the seashore";
string s8 = "seashells by the seashore";
cout << "String 7: " << s7 << endl;
cout << "String 8: " << s8 << endl;
if(checkAnagrams(s7, s8))
{
cout << "Yes, the two strings are anagrams of each other" << endl;
}
else
{
cout << "No, the two strings are not anagrams of each other" << endl;
}
string s9 = "creative";
string s10 = "reactive";
cout << "String 9: " << s9 << endl;
cout << "String 10: " << s10 << endl;
if(checkAnagrams(s9, s10))
{
cout << "Yes, the two strings are anagrams of each other" << endl;
}
else
{
cout << "No, the two strings are not anagrams of each other" << endl;
}
return 0;
}

输出:

 String 1: listen
String 2: silent
Yes, the two strings are anagrams of each other
String 3: Welcome to MUO
String 4: MUO to Welcome
Yes, the two strings are anagrams of each other
String 5: Peter Piper picked a peck of pickled peppers
String 6: A peck of pickled peppers Peter Piper picked
No, the two strings are not anagrams of each other
String 7: She sells seashells by the seashore
String 8: seashells by the seashore
No, the two strings are not anagrams of each other
String 9: creative
String 10: reactive
Yes, the two strings are anagrams of each other

相关:如何计算字符串中给定字符的出现次数

Python程序检查两个字符串是否互为变位词

下面是检查两个字符串是否互为字谜的 Python 程序:

 def checkAnagrams(s1, s2):
size1 = len(s1)
size2 = len(s2)
# If the length of both strings are not the same,
# it means they can't be anagrams of each other.
# Thus, return false.
if size1 != size2:
return 0
s1 = sorted(s1)
s2 = sorted(s2)
for i in range(0, size1):
if s1[i] != s2[i]:
return False
return True

s1 = "listen"
s2 = "silent"
print("String 1: ", s1)
print("String 2: ", s2)
if(checkAnagrams(s1, s2)):
print("Yes, the two strings are anagrams of each other")
else:
print("No, the two strings are not anagrams of each other")
s3 = "Welcome to MUO"
s4 = "MUO to Welcome"
print("String 3: ", s3)
print("String 4: ", s4)
if(checkAnagrams(s3, s4)):
print("Yes, the two strings are anagrams of each other")
else:
print("No, the two strings are not anagrams of each other")
s5 = "Peter Piper picked a peck of pickled peppers"
s6 = "A peck of pickled peppers Peter Piper picked"
print("String 5: ", s5)
print("String 6: ", s6)
if(checkAnagrams(s5, s6)):
print("Yes, the two strings are anagrams of each other")
else:
print("No, the two strings are not anagrams of each other")
s7 = "She sells seashells by the seashore"
s8 = "seashells by the seashore"
print("String 7: ", s7)
print("String 8: ", s8)
if(checkAnagrams(s7, s8)):
print("Yes, the two strings are anagrams of each other")
else:
print("No, the two strings are not anagrams of each other")
s9 = "creative"
s10 = "reactive"
print("String 9: ", s9)
print("String 10: ", s10)
if(checkAnagrams(s9, s10)):
print("Yes, the two strings are anagrams of each other")
else:
print("No, the two strings are not anagrams of each other")

输出:

 String 1: listen
String 2: silent
Yes, the two strings are anagrams of each other
String 3: Welcome to MUO
String 4: MUO to Welcome
Yes, the two strings are anagrams of each other
String 5: Peter Piper picked a peck of pickled peppers
String 6: A peck of pickled peppers Peter Piper picked
No, the two strings are not anagrams of each other
String 7: She sells seashells by the seashore
String 8: seashells by the seashore
No, the two strings are not anagrams of each other
String 9: creative
String 10: reactive
Yes, the two strings are anagrams of each other

相关:如何在字符串中查找元音、辅音、数字和特殊字符

在 JavaScript 中检查两个字符串是否互为变位词

下面是用于检查两个字符串是否互为字谜的 JavaScript 程序:

 function checkAnagrams(s1, s2) {
let size1 = s1.length;
let size2 = s2.length;
// If the length of both strings are not the same,
// it means they can't be anagrams of each other.
// Thus, return false.
if (size1 != size2)
{
return false;
}
s1.sort();
s2.sort();
for (let i = 0; i < size1; i++)
{
if (s1[i] != s2[i])
{
return false;
}
}
return true;
}

var s1 = "listen";
var s2 = "silent";
document.write("String 1: " + s1 + "<br>");
document.write("String 2: " + s2 + "<br>");
if(checkAnagrams(s1.split(""), s2.split(""))) {
document.write("Yes, the two strings are anagrams of each other" + "<br>");
} else {
document.write("No, the two strings are not anagrams of each other" + "<br>");
}
var s3 = "Welcome to MUO";
var s4 = "MUO to Welcome";
document.write("String 3: " + s3 + "<br>");
document.write("String 4: " + s4 + "<br>");
if(checkAnagrams(s3.split(""), s4.split(""))) {
document.write("Yes, the two strings are anagrams of each other" + "<br>");
} else {
document.write("No, the two strings are not anagrams of each other" + "<br>");
}
var s5 = "Peter Piper picked a peck of pickled peppers";
var s6 = "A peck of pickled peppers Peter Piper picked";
document.write("String 5: " + s5 + "<br>");
document.write("String 6: " + s6 + "<br>");
if(checkAnagrams(s5.split(""), s6.split(""))) {
document.write("Yes, the two strings are anagrams of each other" + "<br>");
} else {
document.write("No, the two strings are not anagrams of each other" + "<br>");
}
var s7 = "She sells seashells by the seashore";
var s8 = "seashells by the seashore";
document.write("String 7: " + s7 + "<br>");
document.write("String 8: " + s8 + "<br>");
if(checkAnagrams(s7.split(""), s8.split(""))) {
document.write("Yes, the two strings are anagrams of each other" + "<br>");
} else {
document.write("No, the two strings are not anagrams of each other" + "<br>");
}
var s9 = "creative";
var s10 = "reactive";
document.write("String 9: " + s9 + "<br>");
document.write("String 10: " + s10 + "<br>");
if(checkAnagrams(s9.split(""), s10.split(""))) {
document.write("Yes, the two strings are anagrams of each other" + "<br>");
} else {
document.write("No, the two strings are not anagrams of each other" + "<br>");
}

输出:

 String 1: listen
String 2: silent
Yes, the two strings are anagrams of each other
String 3: Welcome to MUO
String 4: MUO to Welcome
Yes, the two strings are anagrams of each other
String 5: Peter Piper picked a peck of pickled peppers
String 6: A peck of pickled peppers Peter Piper picked
No, the two strings are not anagrams of each other
String 7: She sells seashells by the seashore
String 8: seashells by the seashore
No, the two strings are not anagrams of each other
String 9: creative
String 10: reactive
Yes, the two strings are anagrams of each other

相关:你如何找到一个字符的 ASCII 值?

使用正确的资源来学习编码

如果您想巩固自己的编码技能,学习新概念并花时间使用它们很重要。一种方法是使用编程应用程序,这将帮助您在享受乐趣的同时学习不同的编程概念。