Paano Suriin kung Ang Dalawang Mga String Ay Mga Anagram ng bawat Isa

Paano Suriin kung Ang Dalawang Mga String Ay Mga Anagram ng bawat Isa

Ang isang anagram ay isang string na nabuo sa pamamagitan ng pag-aayos ng mga titik ng isang iba't ibang mga string. Ang pagsuri kung ang dalawang mga string ay anagram ng bawat isa ay maaaring mahirap pakinggan, ngunit ito ay isang maliit lamang nakakalito at mapanlinlang na prangka. Sa artikulong ito, malalaman mo kung paano suriin kung ang dalawang mga string ay anagram ng bawat isa gamit ang C ++, Python, at JavaScript.





Pahayag ng Suliranin

Binibigyan ka ng dalawang mga string s1 at s2, kailangan mong suriin kung ang dalawang mga string ay anagram ng bawat isa o hindi.





Halimbawa 1 : Hayaan ang s1 = 'malikhain' at s2 = 'reaktibo'.





Dahil ang pangalawang string ay maaaring mabuo sa pamamagitan ng muling pagsasaayos ng mga titik ng unang string at kabaligtaran, sa gayon ang dalawang mga string ay anagram ng bawat isa.

Halimbawa 2 : Hayaan ang s1 = 'Peter Piper pumili ng isang peck ng adobo peppers' at s2 = 'Isang peck ng adobo peppers Peter Piper pumili'.



Dahil ang pangalawang string ay hindi maaaring mabuo sa pamamagitan ng muling pagsasaayos ng mga titik ng unang string at kabaligtaran, sa gayon ang dalawang mga string ay hindi mga anagram sa bawat isa.

Proseso Para sa Pagsuri kung Dalawang Mga String Ay Mga Anagram ng bawat Isa

Maaari mong sundin ang diskarte sa ibaba upang suriin kung ang dalawang mga string ay anagram ng bawat isa:





  1. Ihambing ang haba ng parehong mga string.
  2. Kung ang haba ng parehong mga string ay hindi pareho, nangangahulugan ito na hindi sila maaaring maging anagram ng bawat isa. Kaya, bumalik maling.
  3. Kung ang haba ng parehong mga string ay pareho, magpatuloy pa.
  4. Pagbukud-bukurin ang parehong mga string.
  5. Ihambing ang parehong pinagsunod-sunod na mga string.
  6. Kung pareho ang pinagsunod-sunod na mga string ay pareho, nangangahulugan ito na sila ay mga anagram ng bawat isa. Kaya, bumalik totoo.
  7. Kung magkakaiba ang mga pinagsunod-sunod na mga string, nangangahulugan ito na hindi sila anagram ng bawat isa. Kaya, bumalik maling.

Kaugnay: Paano Suriin kung ang Isang String Ay Isang Palindrome

C ++ Program upang Suriin Kung Dalawang Mga String Ay Mga Anagram ng bawat Isa

Nasa ibaba ang programa ng C ++ upang suriin kung ang dalawang mga string ay anagram ng bawat isa o hindi:





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

Output:

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

Kaugnay: Paano Bilangin ang Mga Nangyayari ng isang Naibigay na Character sa isang String

Python Program upang Suriin Kung Ang Dalawang Mga String Ay Mga Anagram ng bawat Isa

Nasa ibaba ang programa ng Python upang suriin kung ang dalawang mga string ay anagram ng bawat isa o hindi:

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

Output:

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

Kaugnay: Paano Makahanap ng Mga Vowel, Consonant, Digit, at Mga Espesyal na Character sa isang String

Suriin Kung Ang Dalawang Mga String Ay Anagrams ng bawat Isa sa JavaScript

Nasa ibaba ang programa ng JavaScript upang suriin kung ang dalawang mga string ay anagram ng bawat isa o hindi:

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

Output:

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

Kaugnay: Paano Mo Mahahanap ang Halaga ng ASCII Ng Isang Character?

Gumamit ng Tamang Mga Mapagkukunan upang Matuto sa Code

Kung nais mong patatagin ang iyong mga kasanayan sa pag-coding, mahalagang malaman ang mga bagong konsepto at gugugol ng oras sa paggamit ng mga ito. Ang isang paraan upang magawa ito ay sa mga apps ng programa, na makakatulong sa iyo na matuto ng iba't ibang mga konsepto ng pagprograma habang masaya sa parehong oras.

Magbahagi Magbahagi Mag-tweet Email 8 Mga App upang Matulungan kang Matuto sa Code para sa Araw ng Mga Programmer ng Internasyonal

Nais mong palakasin ang iyong mga kasanayan sa pag-coding? Tutulungan ka ng mga app at website na ito na malaman ang pag-program sa iyong sariling bilis.

kung paano gamitin ang gpu tweak 2
Basahin Susunod Mga Kaugnay na Paksa
  • Programming
  • JavaScript
  • Sawa
  • C Programming
Tungkol sa May-akda Yuvraj Chandra(60 Mga Artikulo Na-publish)

Si Yuvraj ay isang undergraduate na mag-aaral sa Computer Science sa University of Delhi, India. Masigasig siya sa Full Stack Web Development. Kapag hindi siya nagsusulat, sinisiyasat niya ang lalim ng iba't ibang mga teknolohiya.

Higit pa Mula kay Yuvraj Chandra

Mag-subscribe sa aming newsletter

Sumali sa aming newsletter para sa mga tip sa tech, pagsusuri, libreng ebook, at eksklusibong deal!

Mag-click dito upang mag-subscribe