Πώς να ελέγξετε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους

Πώς να ελέγξετε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους

Το anagram είναι μια συμβολοσειρά που σχηματίζεται με αναδιάταξη των γραμμάτων μιας διαφορετικής συμβολοσειράς. Ο έλεγχος αν δύο χορδές είναι αναγράμματα μεταξύ τους μπορεί να ακούγεται δύσκολο, αλλά είναι μόνο λίγο περίπλοκο και παραπλανητικά απλό. Σε αυτό το άρθρο, θα μάθετε πώς μπορείτε να ελέγξετε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους χρησιμοποιώντας C ++, Python και JavaScript.





Δήλωση Προβλήματος

Σας δίνονται δύο συμβολοσειρές s1 και s2, πρέπει να ελέγξετε αν οι δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους ή όχι.





Παράδειγμα 1 : Έστω s1 = 'δημιουργικό' και s2 = 'αντιδραστικό'.





Δεδομένου ότι η δεύτερη συμβολοσειρά μπορεί να σχηματιστεί με αναδιάταξη των γραμμάτων της πρώτης συμβολοσειράς και αντίστροφα, έτσι οι δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους.

Παράδειγμα 2 : Αφήστε s1 = 'Ο Peter Piper μάζεψε μια πιπεριά τουρσί πιπεριές' και s2 = 'Ένα peck πιπεριές τουρσί πήρε ο Peter Piper'.



Δεδομένου ότι η δεύτερη συμβολοσειρά δεν μπορεί να σχηματιστεί αναδιατάσσοντας τα γράμματα της πρώτης συμβολοσειράς και αντίστροφα, έτσι οι δύο συμβολοσειρές δεν είναι αναγράμματα μεταξύ τους.

Διαδικασία για έλεγχο αν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους

Μπορείτε να ακολουθήσετε την παρακάτω προσέγγιση για να ελέγξετε εάν οι δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους:





  1. Συγκρίνετε το μήκος και των δύο χορδών.
  2. Εάν το μήκος και των δύο συμβολοσειρών δεν είναι το ίδιο, σημαίνει ότι δεν μπορούν να είναι αναγράμματα μεταξύ τους. Έτσι, επιστρέψτε το false.
  3. Εάν το μήκος και των δύο συμβολοσειρών είναι το ίδιο, προχωρήστε περαιτέρω.
  4. Ταξινόμηση και των δύο συμβολοσειρών.
  5. Συγκρίνετε και τις δύο ταξινομημένες συμβολοσειρές.
  6. Εάν και οι δύο ταξινομημένες συμβολοσειρές είναι ίδιες, σημαίνει ότι είναι αναγράμματα μεταξύ τους. Έτσι, επιστρέψτε αληθινό.
  7. Εάν και οι δύο ταξινομημένες συμβολοσειρές είναι διαφορετικές, σημαίνει ότι δεν είναι αναγράμματα μεταξύ τους. Έτσι, επιστρέψτε το false.

Σχετίζεται με: Πώς να ελέγξετε αν μια συμβολοσειρά είναι Palindrome

Πρόγραμμα 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

Σχετικά: Πώς να μετρήσετε τις εμφανίσεις ενός δεδομένου χαρακτήρα σε μια συμβολοσειρά

Πρόγραμμα Python για να ελέγξετε αν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους

Παρακάτω είναι το πρόγραμμα Python για να ελέγξετε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους ή όχι:

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

Σχετικά: Πώς να βρείτε φωνήεντα, σύμφωνα, ψηφία και ειδικούς χαρακτήρες σε μια συμβολοσειρά

Ελέγξτε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους στο JavaScript

Παρακάτω είναι το πρόγραμμα JavaScript για να ελέγξετε εάν δύο συμβολοσειρές είναι αναγράμματα μεταξύ τους ή όχι:

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 εφαρμογές που θα σας βοηθήσουν να μάθετε να κωδικοποιείτε για τη Διεθνή Ημέρα Προγραμματιστών

Θέλετε να βελτιώσετε τις δεξιότητές σας στην κωδικοποίηση; Αυτές οι εφαρμογές και οι ιστότοποι θα σας βοηθήσουν να μάθετε προγραμματισμό με τον δικό σας ρυθμό.

αλλαγή iphone backup location windows 10
Διαβάστε Επόμενο Σχετικά θέματα
  • Προγραμματισμός
  • JavaScript
  • Πύθων
  • Γ Προγραμματισμός
Σχετικά με τον Συγγραφέα Γιουβράι Τσάντρα(Δημοσιεύθηκαν 60 άρθρα)

Ο Yuvraj είναι προπτυχιακός φοιτητής Πληροφορικής στο Πανεπιστήμιο του Δελχί, Ινδία. Είναι παθιασμένος με το Full Stack Web Development. Όταν δεν γράφει, διερευνά το βάθος διαφορετικών τεχνολογιών.

Περισσότερα από τον Yuvraj Chandra

Εγγραφείτε στο newsletter μας

Εγγραφείτε στο ενημερωτικό μας δελτίο για τεχνικές συμβουλές, κριτικές, δωρεάν ebooks και αποκλειστικές προσφορές!

Κάντε κλικ εδώ για εγγραφή