วิธีตรวจสอบว่าสองสตริงเป็นแอนนาแกรมของกันและกันหรือไม่

วิธีตรวจสอบว่าสองสตริงเป็นแอนนาแกรมของกันและกันหรือไม่

แอนนาแกรมเป็นสตริงที่เกิดขึ้นจากการจัดเรียงตัวอักษรของสตริงอื่น การตรวจสอบว่าสายอักขระสองสายเป็นแอนนาแกรมของกันและกันหรือไม่อาจฟังดูยาก แต่ก็ค่อนข้างยุ่งยากและตรงไปตรงมาเพียงเล็กน้อยเท่านั้น ในบทความนี้ คุณจะได้เรียนรู้วิธีตรวจสอบว่าสตริงทั้งสองเป็นแอนนาแกรมของกันและกันหรือไม่โดยใช้ C++, Python และ JavaScript





คำชี้แจงปัญหา

คุณได้รับ s1 และ s2 สองสตริง คุณต้องตรวจสอบว่าสตริงทั้งสองนั้นเป็นแอนนาแกรมของกันและกันหรือไม่





ตัวอย่าง 1 : ให้ s1 = 'creative' และ s2 = 'reactive'





เนื่องจากสตริงที่สองสามารถเกิดขึ้นได้โดยการจัดเรียงตัวอักษรของสตริงแรกและในทางกลับกัน ดังนั้นสตริงทั้งสองจึงเป็นแอนนาแกรมของกันและกัน

ตัวอย่าง 2 ให้ s1 = 'Peter Piper หยิบพริกดองหนึ่งเม็ด' และ s2 = 'พริกดองที่ Peter Piper เลือก'



เนื่องจากสตริงที่สองไม่สามารถเกิดขึ้นได้โดยการจัดเรียงตัวอักษรของสตริงแรกและในทางกลับกัน ดังนั้นสตริงทั้งสองจึงไม่ใช่แอนนาแกรมของกันและกัน

กระบวนการตรวจสอบว่าสองสตริงเป็นแอนนาแกรมของกันและกันหรือไม่

คุณสามารถปฏิบัติตามแนวทางด้านล่างเพื่อตรวจสอบว่าสตริงทั้งสองเป็นแอนนาแกรมของกันและกันหรือไม่:





  1. เปรียบเทียบความยาวของสายทั้งสอง
  2. หากความยาวของทั้งสองสตริงไม่เท่ากัน แสดงว่าไม่สามารถเป็นแอนนาแกรมของกันและกันได้ ดังนั้นให้คืนค่าเท็จ
  3. หากความยาวของทั้งสองสายเท่ากัน ให้ดำเนินการต่อไป
  4. เรียงลำดับทั้งสองสตริง
  5. เปรียบเทียบทั้งสองสตริงที่เรียงลำดับ
  6. หากสตริงที่เรียงลำดับทั้งสองเหมือนกัน แสดงว่าเป็นแอนนาแกรมของกันและกัน ดังนั้นกลับเป็นจริง
  7. หากสตริงที่จัดเรียงทั้งสองต่างกัน แสดงว่าไม่ใช่แอนนาแกรมของกันและกัน ดังนั้นให้คืนค่าเท็จ

ที่เกี่ยวข้อง: วิธีตรวจสอบว่าสตริงเป็น Palindrome หรือไม่

โปรแกรม C++ เพื่อตรวจสอบว่าสองสตริงเป็นแอนนาแกรมของกันและกันหรือไม่

ด้านล่างนี้คือโปรแกรม C++ เพื่อตรวจสอบว่าสองสตริงเป็นแอนนาแกรมของกันและกันหรือไม่:





#include
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 {
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 {
if (s1[i] != s2[i])
{
return false;
}
}
return true;
}

var s1 = 'listen';
var s2 = 'silent';
document.write('String 1: ' + s1 + '
');
document.write('String 2: ' + s2 + '
');
if(checkAnagrams(s1.split(''), s2.split(''))) {
document.write('Yes, the two strings are anagrams of each other' + '
');
} else {
document.write('No, the two strings are not anagrams of each other' + '
');
}
var s3 = 'Welcome to MUO';
var s4 = 'MUO to Welcome';
document.write('String 3: ' + s3 + '
');
document.write('String 4: ' + s4 + '
');
if(checkAnagrams(s3.split(''), s4.split(''))) {
document.write('Yes, the two strings are anagrams of each other' + '
');
} else {
document.write('No, the two strings are not anagrams of each other' + '
');
}
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 + '
');
document.write('String 6: ' + s6 + '
');
if(checkAnagrams(s5.split(''), s6.split(''))) {
document.write('Yes, the two strings are anagrams of each other' + '
');
} else {
document.write('No, the two strings are not anagrams of each other' + '
');
}
var s7 = 'She sells seashells by the seashore';
var s8 = 'seashells by the seashore';
document.write('String 7: ' + s7 + '
');
document.write('String 8: ' + s8 + '
');
if(checkAnagrams(s7.split(''), s8.split(''))) {
document.write('Yes, the two strings are anagrams of each other' + '
');
} else {
document.write('No, the two strings are not anagrams of each other' + '
');
}
var s9 = 'creative';
var s10 = 'reactive';
document.write('String 9: ' + s9 + '
');
document.write('String 10: ' + s10 + '
');
if(checkAnagrams(s9.split(''), s10.split(''))) {
document.write('Yes, the two strings are anagrams of each other' + '
');
} else {
document.write('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

ที่เกี่ยวข้อง: คุณจะหาค่า ASCII ของตัวละครได้อย่างไร?

ใช้แหล่งข้อมูลที่เหมาะสมเพื่อเรียนรู้การเขียนโค้ด

หากคุณต้องการเสริมสร้างทักษะการเขียนโค้ดของคุณ สิ่งสำคัญคือต้องเรียนรู้แนวคิดใหม่และใช้เวลากับแนวคิดเหล่านี้ วิธีหนึ่งในการทำเช่นนี้คือการใช้แอพเขียนโปรแกรม ซึ่งจะช่วยให้คุณเรียนรู้แนวคิดการเขียนโปรแกรมที่แตกต่างกันไปพร้อมกับความสนุกสนานไปพร้อม ๆ กัน

แบ่งปัน แบ่งปัน ทวีต อีเมล 8 แอพที่จะช่วยให้คุณเรียนรู้การเขียนโค้ดสำหรับวันโปรแกรมเมอร์สากล

ต้องการฝึกฝนทักษะการเขียนโค้ดของคุณหรือไม่? แอพและเว็บไซต์เหล่านี้จะช่วยให้คุณเรียนรู้การเขียนโปรแกรมตามที่คุณต้องการ

wifi ไม่มีการกำหนดค่าที่ถูกต้อง
อ่านต่อไป หัวข้อที่เกี่ยวข้อง
  • การเขียนโปรแกรม
  • JavaScript
  • Python
  • การเขียนโปรแกรม C
เกี่ยวกับผู้เขียน ยุวราช จันทรา(60 บทความที่ตีพิมพ์)

Yuvraj เป็นนักศึกษาระดับปริญญาตรีสาขาวิทยาการคอมพิวเตอร์ที่มหาวิทยาลัยเดลี ประเทศอินเดีย เขาหลงใหลเกี่ยวกับ Full Stack Web Development เมื่อไม่ได้เขียน เขากำลังสำรวจความลึกของเทคโนโลยีต่างๆ

เพิ่มเติมจาก Yuvraj Chandra

สมัครรับจดหมายข่าวของเรา

เข้าร่วมจดหมายข่าวของเราสำหรับเคล็ดลับทางเทคนิค บทวิจารณ์ eBook ฟรี และดีลพิเศษ!

คลิกที่นี่เพื่อสมัครสมาชิก