कैसे जांचें कि क्या दो तार एक दूसरे के विपर्यय हैं?

कैसे जांचें कि क्या दो तार एक दूसरे के विपर्यय हैं?

एक विपर्यय एक अलग स्ट्रिंग के अक्षरों को पुनर्व्यवस्थित करके बनाई गई एक स्ट्रिंग है। यह जाँचना कि क्या दो तार एक-दूसरे के विपर्यय हैं, मुश्किल लग सकता है, लेकिन यह केवल थोड़ा मुश्किल और भ्रामक रूप से सीधा है। इस लेख में, आप सीखेंगे कि कैसे जांचें कि क्या दो तार 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

सम्बंधित: आप किसी वर्ण का ASCII मान कैसे ज्ञात करते हैं?

कोड सीखने के लिए सही संसाधनों का उपयोग करें

यदि आप अपने कोडिंग कौशल को मजबूत करना चाहते हैं, तो नई अवधारणाओं को सीखना और उनका उपयोग करके समय बिताना महत्वपूर्ण है। ऐसा करने का एक तरीका प्रोग्रामिंग ऐप्स के साथ है, जो आपको एक ही समय में मज़े करते हुए विभिन्न प्रोग्रामिंग अवधारणाओं को सीखने में मदद करेगा।

साझा करना साझा करना कलरव ईमेल अंतर्राष्ट्रीय प्रोग्रामर दिवस के लिए कोड सीखने में आपकी मदद करने के लिए 8 ऐप्स

अपने कोडिंग कौशल को ब्रश करना चाहते हैं? ये ऐप और वेबसाइट आपको अपनी गति से प्रोग्रामिंग सीखने में मदद करेंगे।

स्नेस क्लासिक पर नेस गेम खेलें
आगे पढ़िए संबंधित विषय
  • प्रोग्रामिंग
  • जावास्क्रिप्ट
  • अजगर
  • सी प्रोग्रामिंग
लेखक के बारे में युवराज चंद्र(60 लेख प्रकाशित)

युवराज दिल्ली विश्वविद्यालय, भारत में कंप्यूटर विज्ञान के स्नातक छात्र हैं। उन्हें फुल स्टैक वेब डेवलपमेंट का शौक है। जब वह नहीं लिख रहा होता है, तो वह विभिन्न तकनीकों की गहराई की खोज कर रहा होता है।

युवराज चंद्र की अन्य फ़िल्में-टीवी शो

हमारे न्यूज़लेटर की सदस्यता लें

तकनीकी युक्तियों, समीक्षाओं, निःशुल्क ई-पुस्तकों और अनन्य सौदों के लिए हमारे न्यूज़लेटर से जुड़ें!

सब्सक्राइब करने के लिए यहां क्लिक करें