দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা কীভাবে পরীক্ষা করবেন

দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা কীভাবে পরীক্ষা করবেন

একটি অ্যানগ্রাম হল একটি স্ট্রিং যা একটি ভিন্ন স্ট্রিং এর অক্ষরগুলিকে পুনর্বিন্যাস করে গঠিত হয়। দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা যাচাই করা কঠিন মনে হতে পারে, তবে এটি কেবল একটু জটিল এবং প্রতারণামূলকভাবে সহজবোধ্য। এই নিবন্ধে, আপনি C ++, পাইথন এবং জাভাস্ক্রিপ্ট ব্যবহার করে দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা পরীক্ষা করতে শিখবেন।





সমস্যা বিবৃতি

আপনাকে দুটি স্ট্রিং s1 এবং s2 দেওয়া হয়েছে, আপনাকে দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা পরীক্ষা করতে হবে।





উদাহরণ 1 : যাক s1 = 'সৃজনশীল' এবং s2 = 'প্রতিক্রিয়াশীল'।





যেহেতু দ্বিতীয় স্ট্রিংটি প্রথম স্ট্রিং এর অক্ষরগুলিকে পুনর্বিন্যাস করে এবং বিপরীতভাবে গঠিত হতে পারে, এইভাবে দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম।

উদাহরণ 2 : এস 1 = 'পিটার পাইপার আচারযুক্ত মরিচের একটি পিক বাছাই করুন' এবং s2 = 'পিকেল পিপার পিক পিপার'



যেহেতু দ্বিতীয় স্ট্রিংটি প্রথম স্ট্রিং এর অক্ষরগুলিকে পুনর্বিন্যাস করে তৈরি করা যায় না এবং বিপরীতভাবে, এইভাবে দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম নয়।

দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা পরীক্ষা করার প্রক্রিয়া

দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা পরীক্ষা করার জন্য আপনি নীচের পদ্ধতিটি অনুসরণ করতে পারেন:





  1. উভয় স্ট্রিং এর দৈর্ঘ্য তুলনা করুন।
  2. যদি উভয় স্ট্রিংগুলির দৈর্ঘ্য একই না হয়, তবে এর অর্থ হল তারা একে অপরের অ্যানাগ্রাম হতে পারে না। এভাবে, মিথ্যা ফেরত দিন।
  3. যদি উভয় স্ট্রিং এর দৈর্ঘ্য একই হয়, আরও এগিয়ে যান।
  4. উভয় স্ট্রিং সাজান।
  5. উভয় সাজানো স্ট্রিং তুলনা করুন।
  6. যদি উভয় সাজানো স্ট্রিং একই হয়, এর মানে হল তারা একে অপরের অ্যানাগ্রাম। সুতরাং, সত্য ফিরে।
  7. যদি উভয় সাজানো স্ট্রিং আলাদা হয়, তাহলে এর মানে হল যে তারা একে অপরের অ্যানাগ্রাম নয়। সুতরাং, মিথ্যা ফেরত দিন।

সম্পর্কিত: স্ট্রিং প্যালিনড্রোম কিনা তা কীভাবে পরীক্ষা করবেন

দুটি স্ট্রিং একে অপরের অ্যানাগ্রাম কিনা তা পরীক্ষা করার জন্য 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 মান খুঁজে পাবেন?

কোড শিখতে সঠিক সম্পদ ব্যবহার করুন

আপনি যদি আপনার কোডিং দক্ষতাকে দৃ solid় করতে চান, নতুন ধারণাগুলি শিখতে এবং সেগুলি ব্যবহার করে সময় ব্যয় করা গুরুত্বপূর্ণ। এটি করার একটি উপায় হল প্রোগ্রামিং অ্যাপস, যা আপনাকে একই সাথে মজা করার সময় বিভিন্ন প্রোগ্রামিং ধারণা শিখতে সাহায্য করবে।

শেয়ার করুন শেয়ার করুন টুইট ইমেইল আন্তর্জাতিক প্রোগ্রামার দিবসের জন্য কোড শিখতে আপনাকে সাহায্য করার জন্য 8 টি অ্যাপ

আপনার কোডিং দক্ষতা বাড়াতে চান? এই অ্যাপস এবং ওয়েবসাইটগুলি আপনাকে আপনার নিজস্ব গতিতে প্রোগ্রামিং শিখতে সাহায্য করবে।

কিভাবে ফেসবুক বিজনেস পেজ ডিলিট করবেন
পরবর্তী পড়ুন সম্পর্কিত বিষয়
  • প্রোগ্রামিং
  • জাভাস্ক্রিপ্ট
  • পাইথন
  • সি প্রোগ্রামিং
লেখক সম্পর্কে যুবরাজ চন্দ্র(60 নিবন্ধ প্রকাশিত)

যুবরাজ ভারতের দিল্লি বিশ্ববিদ্যালয়ের কম্পিউটার সায়েন্সের স্নাতক ছাত্র। তিনি ফুল স্ট্যাক ওয়েব ডেভেলপমেন্ট সম্পর্কে উত্সাহী। যখন তিনি লিখছেন না, তিনি বিভিন্ন প্রযুক্তির গভীরতা অন্বেষণ করছেন।

যুবরাজ চন্দ্রের কাছ থেকে আরো

আমাদের নিউজলেটার সদস্যতা

প্রযুক্তি টিপস, রিভিউ, ফ্রি ইবুক এবং এক্সক্লুসিভ ডিলের জন্য আমাদের নিউজলেটারে যোগ দিন!

সাবস্ক্রাইব করতে এখানে ক্লিক করুন