Cách kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không

Cách kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không

Đảo chữ cái là một chuỗi được hình thành bằng cách sắp xếp lại các chữ cái của một chuỗi khác. Kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không nghe có vẻ khó khăn, nhưng nó chỉ hơi phức tạp và đơn giản. Trong bài viết này, bạn sẽ học cách kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không bằng cách sử dụng C ++, Python và JavaScript.





Báo cáo vấn đề

Bạn được cung cấp hai chuỗi s1 và s2, bạn cần phải kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không.





ví dụ 1 : Cho s1 = 'creative' và s2 = 'react'.





Vì chuỗi thứ hai có thể được hình thành bằng cách sắp xếp lại các chữ cái của chuỗi đầu tiên và ngược lại, do đó hai chuỗi là đảo ngữ của nhau.

Ví dụ 2 : Let s1 = 'Peter Piper đã nhặt một miếng ớt ngâm' và s2 = 'Một miếng ớt ngâm Peter Piper đã chọn'.



Vì chuỗi thứ hai không thể được hình thành bằng cách sắp xếp lại các chữ cái của chuỗi đầu tiên và ngược lại, do đó hai chuỗi không phải là đảo ngữ của nhau.

Quy trình kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không

Bạn có thể làm theo cách tiếp cận bên dưới để kiểm tra xem hai chuỗi có phải là từ đảo ngữ của nhau hay không:





  1. So sánh độ dài của cả hai chuỗi.
  2. Nếu độ dài của cả hai chuỗi không giống nhau, điều đó có nghĩa là chúng không thể đảo ngữ của nhau. Do đó, trả về false.
  3. Nếu độ dài của cả hai chuỗi là như nhau, hãy tiếp tục.
  4. Sắp xếp cả hai chuỗi.
  5. So sánh cả hai chuỗi đã sắp xếp.
  6. Nếu cả hai chuỗi được sắp xếp đều giống nhau, điều đó có nghĩa là chúng là đảo ngữ của nhau. Do đó, trả về true.
  7. Nếu cả hai chuỗi được sắp xếp đều khác nhau, điều đó có nghĩa là chúng không phải là đảo ngữ của nhau. Do đó, trả về false.

Có liên quan: Cách kiểm tra xem chuỗi có phải là hội chứng Pali hay không

Chương trình C ++ để kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không

Dưới đây là chương trình C ++ để kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không:





#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;
}

Đầu ra:

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

Liên quan: Cách đếm số lần xuất hiện của một ký tự đã cho trong một chuỗi

Chương trình Python để kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không

Dưới đây là chương trình Python để kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không:

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')

Đầu ra:

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

Liên quan: Cách tìm nguyên âm, phụ âm, chữ số và ký tự đặc biệt trong chuỗi

Kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau trong JavaScript hay không

Dưới đây là chương trình JavaScript để kiểm tra xem hai chuỗi có phải là đảo ngữ của nhau hay không:

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' + '
');
}

Đầu ra:

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

Có liên quan: Làm thế nào để bạn tìm thấy giá trị ASCII của một ký tự?

Sử dụng đúng tài nguyên để học viết mã

Nếu bạn đang muốn củng cố kỹ năng viết mã của mình, điều quan trọng là phải học các khái niệm mới và dành thời gian sử dụng chúng. Một cách để làm điều này là sử dụng các ứng dụng lập trình, ứng dụng này sẽ giúp bạn học các khái niệm lập trình khác nhau đồng thời vui vẻ.

Đăng lại Đăng lại tiếng riu ríu E-mail 8 ứng dụng giúp bạn học lập trình nhân Ngày lập trình viên quốc tế

Bạn muốn nâng cao kỹ năng viết mã của mình? Các ứng dụng và trang web này sẽ giúp bạn học lập trình theo tốc độ của riêng mình.

muốn mua từ đó an toàn
Đọc tiếp Chủ đề liên quan
  • Lập trình
  • JavaScript
  • Python
  • Lập trình C
Giới thiệu về tác giả Yuvraj Chandra(60 bài báo đã xuất bản)

Yuvraj là sinh viên ngành Khoa học Máy tính tại Đại học Delhi, Ấn Độ. Anh ấy đam mê Phát triển Web Full Stack. Khi không viết, anh ấy đang khám phá chiều sâu của các công nghệ khác nhau.

Xem thêm từ Yuvraj Chandra

Theo dõi bản tin của chúng tôi

Tham gia bản tin của chúng tôi để biết các mẹo công nghệ, đánh giá, sách điện tử miễn phí và các ưu đãi độc quyền!

Bấm vào đây để đăng ký