Cara Memeriksa sama ada Dua String Merupakan Anagram antara satu sama lain

Cara Memeriksa sama ada Dua String Merupakan Anagram antara satu sama lain

Anagram adalah rentetan yang dibentuk dengan menyusun semula huruf rentetan yang berbeza. Memeriksa sama ada dua tali adalah anagram antara satu sama lain mungkin terdengar sukar, tetapi hanya sedikit rumit dan mudah menipu. Dalam artikel ini, anda akan belajar bagaimana untuk memeriksa sama ada dua rentetan adalah anagram antara satu sama lain menggunakan C ++, Python, dan JavaScript.





Pernyataan masalah

Anda diberi dua rentetan s1 dan s2, anda perlu memeriksa sama ada kedua-dua tali itu adalah anagram satu sama lain atau tidak.





Contoh 1 : Biarkan s1 = 'kreatif' dan s2 = 'reaktif'.





Oleh kerana rentetan kedua dapat dibentuk dengan menyusun kembali huruf rentetan pertama dan sebaliknya, maka kedua tali tersebut merupakan anagram satu sama lain.

Contoh 2 : Biarkan s1 = 'Peter Piper memilih sebatang lada acar' dan s2 = 'Seketul lada acar Peter Piper memilih'.



Oleh kerana rentetan kedua tidak dapat dibentuk dengan menyusun kembali huruf rentetan pertama dan sebaliknya, maka kedua tali itu bukan anagram satu sama lain.

Proses Untuk Memeriksa Adakah Dua Rentetan Merupakan Anagram antara satu sama lain

Anda boleh mengikuti pendekatan di bawah ini untuk memeriksa sama ada kedua-dua tali itu adalah anagram antara satu sama lain:





  1. Bandingkan panjang kedua-dua tali.
  2. Sekiranya panjang kedua-dua tali tidak sama, ini bermakna kedua-duanya tidak boleh menjadi satu sama lain. Oleh itu, kembali palsu.
  3. Sekiranya panjang kedua tali sama, teruskan lebih jauh.
  4. Susun kedua-dua tali.
  5. Bandingkan kedua-dua rentetan yang disusun.
  6. Sekiranya kedua-dua rentetan yang disusun adalah sama, ini bermaksud anagram masing-masing. Oleh itu, kembali benar.
  7. Sekiranya kedua-dua rentetan yang disusun berbeza, ini bermakna mereka bukan anagram antara satu sama lain. Oleh itu, kembali palsu.

Berkaitan: Cara Memeriksa apakah String Adalah Palindrome

Program C ++ untuk Memeriksa Jika Dua String Merupakan Anagram antara satu sama lain

Berikut adalah program C ++ untuk memeriksa sama ada dua rentetan adalah anagram antara satu sama lain atau tidak:





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

Pengeluaran:

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

Berkaitan: Cara Mengira Kejadian Karakter yang Diberi dalam Rentetan

Program Python untuk Memeriksa Sama ada Dua Tali Adalah Anagram antara satu sama lain

Di bawah ini adalah program Python untuk memeriksa sama ada dua rentetan adalah anagram antara satu sama lain atau tidak:

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

Pengeluaran:

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

Berkaitan: Cara Mencari Vokal, Konsonan, Digit, dan Karakter Khas dalam Rentetan

Periksa Jika Dua String Merupakan Anagram antara satu sama lain dalam JavaScript

Di bawah ini adalah program JavaScript untuk memeriksa sama ada dua rentetan adalah anagram antara satu sama lain atau tidak:

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

Pengeluaran:

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

Berkaitan: Bagaimana Anda Menemui Nilai ASCII Perwatakan?

Gunakan Sumber yang Betul untuk Belajar Mengekod

Sekiranya anda ingin memantapkan kemahiran pengekodan anda, penting untuk mempelajari konsep baru dan menghabiskan masa menggunakannya. Salah satu cara untuk melakukannya adalah dengan aplikasi pengaturcaraan, yang akan membantu anda mempelajari konsep pengaturcaraan yang berbeza sambil bersenang-senang pada masa yang sama.

Berkongsi Berkongsi Tweet E-mel 8 Aplikasi untuk Membantu Anda Belajar Mengenali Hari Pengaturcara Antarabangsa

Ingin meningkatkan kemahiran pengekodan anda? Aplikasi dan laman web ini akan membantu anda mempelajari pengaturcaraan mengikut kadar anda sendiri.

cara memindahkan muzik dari ipod ke iTunes windows 10
Baca Seterusnya Topik-topik yang berkaitan
  • Pengaturcaraan
  • JavaScript
  • Python
  • Pengaturcaraan C
Mengenai Pengarang Yuvraj Chandra(60 Artikel Diterbitkan)

Yuvraj adalah pelajar sarjana Sains Komputer di University of Delhi, India. Dia meminati Pembangunan Web Stack Penuh. Ketika dia tidak menulis, dia meneroka kedalaman teknologi yang berbeza.

Lagi Dari Yuvraj Chandra

Langgan buletin kami

Sertailah buletin kami untuk mendapatkan petua, ulasan, ebook percuma, dan tawaran eksklusif!

Klik di sini untuk melanggan