రెండు తీగలు ఒకదానికొకటి అనగ్రామ్‌లు కాదా అని ఎలా తనిఖీ చేయాలి

రెండు తీగలు ఒకదానికొకటి అనగ్రామ్‌లు కాదా అని ఎలా తనిఖీ చేయాలి

అనాగ్రామ్ అనేది వేరే స్ట్రింగ్ యొక్క అక్షరాలను పునర్వ్యవస్థీకరించడం ద్వారా ఏర్పడే స్ట్రింగ్. రెండు తీగలు ఒకదానికొకటి అనగ్రామ్‌లు కాదా అని తనిఖీ చేయడం కష్టంగా అనిపించవచ్చు, కానీ ఇది కొంచెం గమ్మత్తైనది మరియు మోసపూరితంగా సూటిగా ఉంటుంది. ఈ వ్యాసంలో, C ++, పైథాన్ మరియు జావాస్క్రిప్ట్ ఉపయోగించి రెండు స్ట్రింగ్‌లు ఒకదానికొకటి అనగ్రామ్‌లు కావాలో ఎలా చెక్ చేయాలో మీరు నేర్చుకుంటారు.





సమస్యల నివేదిక

మీకు 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

సంబంధిత: ఒక అక్షరం యొక్క ASCII విలువను మీరు ఎలా కనుగొంటారు?

కోడ్ నేర్చుకోవడానికి సరైన వనరులను ఉపయోగించండి

మీరు మీ కోడింగ్ నైపుణ్యాలను పటిష్టం చేసుకోవాలని చూస్తున్నట్లయితే, కొత్త భావనలను నేర్చుకోవడం మరియు వాటిని ఉపయోగించి సమయాన్ని గడపడం ముఖ్యం. దీన్ని చేయడానికి ఒక మార్గం ప్రోగ్రామింగ్ యాప్‌లు, అదే సమయంలో సరదాగా ఉన్నప్పుడు విభిన్న ప్రోగ్రామింగ్ కాన్సెప్ట్‌లను నేర్చుకోవడంలో మీకు సహాయపడుతుంది.

షేర్ చేయండి షేర్ చేయండి ట్వీట్ ఇమెయిల్ అంతర్జాతీయ ప్రోగ్రామర్స్ డే కోసం కోడ్ నేర్చుకోవడానికి మీకు సహాయపడే 8 యాప్‌లు

మీ కోడింగ్ నైపుణ్యాలను పెంచుకోవాలనుకుంటున్నారా? మీ స్వంత వేగంతో ప్రోగ్రామింగ్ నేర్చుకోవడానికి ఈ యాప్‌లు మరియు వెబ్‌సైట్‌లు మీకు సహాయపడతాయి.

ఫేస్‌బుక్ హ్యాక్ అయినప్పుడు ఏమి చేయాలి
తదుపరి చదవండి సంబంధిత అంశాలు
  • ప్రోగ్రామింగ్
  • జావాస్క్రిప్ట్
  • పైథాన్
  • సి ప్రోగ్రామింగ్
రచయిత గురుంచి యువరాజ్ చంద్ర(60 కథనాలు ప్రచురించబడ్డాయి)

యువరాజ్ భారతదేశంలోని ఢిల్లీ విశ్వవిద్యాలయంలో కంప్యూటర్ సైన్స్ అండర్ గ్రాడ్యుయేట్ విద్యార్థి. అతను పూర్తి స్టాక్ వెబ్ డెవలప్‌మెంట్ పట్ల మక్కువ కలిగి ఉన్నాడు. అతను వ్రాయనప్పుడు, అతను వివిధ సాంకేతికతల లోతును అన్వేషిస్తున్నాడు.

యువరాజ్ చంద్ర నుండి మరిన్ని

మా వార్తాలేఖకు సభ్యత్వాన్ని పొందండి

టెక్ చిట్కాలు, సమీక్షలు, ఉచిత ఈబుక్‌లు మరియు ప్రత్యేకమైన డీల్స్ కోసం మా వార్తాలేఖలో చేరండి!

సభ్యత్వం పొందడానికి ఇక్కడ క్లిక్ చేయండి