இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்கள் என்பதை எவ்வாறு சரிபார்க்கலாம்

இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்கள் என்பதை எவ்வாறு சரிபார்க்கலாம்

அனாகிராம் என்பது வேறு சரத்தின் எழுத்துக்களை மறுசீரமைப்பதன் மூலம் உருவாக்கப்பட்ட ஒரு சரம். இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்களா என்று சோதிப்பது கடினமாகத் தோன்றலாம், ஆனால் இது கொஞ்சம் தந்திரமான மற்றும் ஏமாற்றும் நேரடியானது. இந்த கட்டுரையில், C ++, Python மற்றும் JavaScript ஐப் பயன்படுத்தி இரண்டு சரங்கள் ஒன்றுக்கொன்று அனகிராம்கள் என்பதை எவ்வாறு சரிபார்க்கலாம் என்பதை நீங்கள் கற்றுக் கொள்வீர்கள்.





பிரச்சனை அறிக்கை

உங்களுக்கு s1 மற்றும் s2 ஆகிய இரண்டு சரங்கள் கொடுக்கப்பட்டுள்ளன, இரண்டு சரங்களும் ஒன்றுக்கொன்று அனகிராம்களா இல்லையா என்பதை நீங்கள் சரிபார்க்க வேண்டும்.





உதாரணம் 1 : S1 = 'ஆக்கப்பூர்வமாக' மற்றும் s2 = 'எதிர்வினை' ஆகட்டும்.





முதல் சரத்தின் எழுத்துக்களை மறுசீரமைப்பதன் மூலம் இரண்டாவது சரம் உருவாக்கப்படலாம் என்பதால், இரண்டு சரங்களும் ஒன்றுக்கொன்று அனகிராம்கள்.

உதாரணம் 2 : S1 = 'பீட்டர் பைபர் ஊறுகாய் மிளகின் ஒரு பெக் எடுத்தார்' மற்றும் s2 = 'ஒரு பெக் ஊறுகாய் மிளகு பீட்டர் பைபர் எடுத்தார்'.



முதல் சரத்தின் எழுத்துக்களை மறுசீரமைப்பதன் மூலம் இரண்டாவது சரத்தை உருவாக்க முடியாது என்பதால், இரண்டு சரங்களும் ஒன்றோடொன்று அனகிராம்கள் அல்ல.

இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்கள் என்பதைச் சரிபார்க்கும் செயல்முறை

இரண்டு சரங்களும் ஒன்றோடொன்று அனகிராம்களாக இருக்கிறதா என்று சோதிக்க கீழே உள்ள அணுகுமுறையைப் பின்பற்றலாம்:





  1. இரண்டு சரங்களின் நீளத்தையும் ஒப்பிடுக.
  2. இரண்டு சரங்களின் நீளம் ஒரே மாதிரியாக இல்லை என்றால், அவை ஒன்றுக்கொன்று அனகிராமாக இருக்க முடியாது. எனவே, தவறாக திரும்பவும்.
  3. இரண்டு சரங்களின் நீளம் ஒரே மாதிரியாக இருந்தால், மேலும் தொடரவும்.
  4. இரண்டு சரங்களையும் வரிசைப்படுத்துங்கள்.
  5. இரண்டு வரிசைப்படுத்தப்பட்ட சரங்களையும் ஒப்பிடுக.
  6. வரிசைப்படுத்தப்பட்ட இரண்டு சரங்களும் ஒரே மாதிரியாக இருந்தால், அவை ஒருவருக்கொருவர் அனகிராம்கள் என்று அர்த்தம். எனவே, உண்மை திரும்ப.
  7. வரிசைப்படுத்தப்பட்ட இரண்டு சரங்களும் வேறுபட்டிருந்தால், அவை ஒருவருக்கொருவர் அனகிராம்கள் அல்ல என்று அர்த்தம். எனவே, தவறாக திரும்பவும்.

தொடர்புடையது: ஒரு சரம் ஒரு பாலிண்ட்ரோம் என்பதை எவ்வாறு சரிபார்க்கலாம்

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

தொடர்புடையது: ஒரு சரத்தில் கொடுக்கப்பட்ட கதாபாத்திரத்தின் நிகழ்வுகளை எவ்வாறு கணக்கிடுவது

இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்கள் என்பதை சரிபார்க்க பைதான் திட்டம்

இரண்டு சரங்கள் ஒன்றுக்கொன்று அனகிராம்களா இல்லையா என்பதை சரிபார்க்க பைதான் திட்டம் கீழே உள்ளது:

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

தொடர்புடையது: ஒரு சரத்தில் உயிர், மெய், இலக்கங்கள் மற்றும் சிறப்பு எழுத்துக்களை எவ்வாறு கண்டுபிடிப்பது

ஜாவாஸ்கிரிப்டில் இரண்டு சரங்கள் ஒருவருக்கொருவர் அனகிராம்கள் என்பதைச் சரிபார்க்கவும்

ஜாவாஸ்கிரிப்ட் புரோகிராம் கீழே இரண்டு சரங்கள் ஒன்றுக்கொன்று அனகிராம்களா இல்லையா என்பதைச் சரிபார்க்க:

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

தொடர்புடையது: ஒரு கதாபாத்திரத்தின் ஆஸ்கி மதிப்பை எப்படி கண்டுபிடிப்பது?

குறியீட்டைக் கற்றுக்கொள்ள சரியான ஆதாரங்களைப் பயன்படுத்தவும்

உங்கள் குறியீட்டுத் திறனை நீங்கள் திடப்படுத்த விரும்பினால், புதிய கருத்துக்களைக் கற்றுக்கொள்வதும் அவற்றைப் பயன்படுத்தி நேரத்தை செலவிடுவதும் முக்கியம். இதைச் செய்வதற்கான ஒரு வழி நிரலாக்க பயன்பாடுகள் ஆகும், இது ஒரே நேரத்தில் வேடிக்கையாக இருக்கும்போது வெவ்வேறு நிரலாக்கக் கருத்துகளைக் கற்றுக்கொள்ள உதவும்.

பகிர் பகிர் ட்வீட் மின்னஞ்சல் சர்வதேச புரோகிராமர் தினத்திற்கான குறியீட்டை கற்றுக்கொள்ள உதவும் 8 செயலிகள்

உங்கள் குறியீட்டு திறன்களை மேம்படுத்த விரும்புகிறீர்களா? இந்த பயன்பாடுகள் மற்றும் வலைத்தளங்கள் உங்கள் சொந்த வேகத்தில் நிரலாக்கத்தைக் கற்றுக்கொள்ள உதவும்.

2020 அவர்களுக்கு தெரியாமல் ஸ்னாப்சாட்டில் ஸ்கிரீன் ஷாட் செய்வது எப்படி
அடுத்து படிக்கவும் தொடர்புடைய தலைப்புகள்
  • நிரலாக்க
  • ஜாவாஸ்கிரிப்ட்
  • பைதான்
  • சி நிரலாக்க
எழுத்தாளர் பற்றி யுவராஜ் சந்திரா(60 கட்டுரைகள் வெளியிடப்பட்டன)

யுவராஜ் இந்தியாவின் டெல்லி பல்கலைக்கழகத்தில் கணினி அறிவியல் இளங்கலை மாணவர். அவர் முழு ஸ்டாக் வலை மேம்பாட்டில் ஆர்வம் கொண்டவர். அவர் எழுதாதபோது, ​​அவர் பல்வேறு தொழில்நுட்பங்களின் ஆழத்தை ஆராய்கிறார்.

யுவராஜ் சந்திராவின் மேலும்

எங்கள் செய்திமடலுக்கு குழுசேரவும்

தொழில்நுட்ப குறிப்புகள், மதிப்புரைகள், இலவச மின் புத்தகங்கள் மற்றும் பிரத்யேக ஒப்பந்தங்களுக்கு எங்கள் செய்திமடலில் சேரவும்!

குழுசேர இங்கே சொடுக்கவும்