Kaip patikrinti, ar dvi eilutės yra viena kitos gramos

Kaip patikrinti, ar dvi eilutės yra viena kitos gramos

Anagrama yra eilutė, sudaryta perstatant kitos eilutės raides. Patikrinti, ar dvi eilutės yra viena kitos anagramos, gali atrodyti sudėtinga, tačiau tai tik šiek tiek sudėtinga ir apgaulingai paprasta. Šiame straipsnyje sužinosite, kaip patikrinti, ar dvi eilutės yra viena kitos anagramos, naudojant „C ++“, „Python“ ir „JavaScript“.





Problemos pareiškimas

Jums suteikiamos dvi eilutės s1 ir s2, turite patikrinti, ar abi eilutės yra viena kitos anagramos, ar ne.





1 pavyzdys : Tegul s1 = 'kūrybingas', o s2 = 'reaktyvus'.





Kadangi antroji eilutė gali būti suformuota pertvarkant pirmosios eilutės raides ir atvirkščiai, abi eilutės yra viena kitos anagramos.

2 pavyzdys : Leiskite s1 = 'Peteris Piperis nuskynė marinuotų pipirų gabaliuką' ir s2 = 'Marinuotų pipirų gabaliuką Petras Piperis nuskynė'.



Kadangi antrosios eilutės negalima suformuoti pertvarkant pirmosios eilutės raides ir atvirkščiai, šios dvi eilutės nėra viena kitos anagramos.

Procesas, skirtas patikrinti, ar dvi eilutės yra viena kitos gramogramos

Galite patikrinti toliau pateiktą metodą ir patikrinti, ar abi eilutės yra viena kitos anagramos:





  1. Palyginkite abiejų stygų ilgį.
  2. Jei abiejų stygų ilgis nėra vienodas, tai reiškia, kad jos negali būti viena kitos anagramos. Taigi grąžinkite klaidą.
  3. Jei abiejų stygų ilgis yra vienodas, tęskite toliau.
  4. Rūšiuokite abi eilutes.
  5. Palyginkite abi surūšiuotas eilutes.
  6. Jei abi rūšiuotos eilutės yra vienodos, tai reiškia, kad jos yra viena kitos anagramos. Taigi, grįžkite tiesa.
  7. Jei abi rūšiuotos eilutės skiriasi, tai reiškia, kad jos nėra viena kitos anagramos. Taigi grąžinkite klaidą.

Susijęs: Kaip patikrinti, ar eilutė yra palindromas

C ++ programa, skirta patikrinti, ar dvi eilutės yra viena kitos gramos

Žemiau yra C ++ programa, skirta patikrinti, ar dvi eilutės yra viena kitos anagramos:





#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;
}

Išėjimas:

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

Susijęs: Kaip skaičiuoti tam tikro simbolio įvykius eilutėje

„Python“ programa, skirta patikrinti, ar dvi eilutės yra viena kitos gramos

Žemiau yra „Python“ programa, skirta patikrinti, ar dvi eilutės yra viena kitos anagramos:

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')

Išėjimas:

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

Susiję: Kaip eilutėje rasti balsių, priebalsių, skaitmenų ir specialiųjų simbolių

Patikrinkite, ar dvi eilutės yra „JavaScript“ viena kitos gramogramos

Žemiau yra „JavaScript“ programa, skirta patikrinti, ar dvi eilutės yra viena kitos anagramos:

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' + '
');
}

Išėjimas:

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

Susijęs: Kaip rasti simbolio ASCII vertę?

Naudokite tinkamus išteklius, kad išmoktumėte koduoti

Jei norite sustiprinti savo kodavimo įgūdžius, svarbu išmokti naujų sąvokų ir praleisti laiką jomis naudojantis. Vienas iš būdų tai padaryti yra programavimo programos, kurios padės jums išmokti skirtingų programavimo koncepcijų, tuo pačiu smagiai.

Dalintis Dalintis „Tweet“ Paštu 8 programos, padėsiančios išmokti koduoti Tarptautinei programuotojų dienai

Norite patobulinti savo kodavimo įgūdžius? Šios programos ir svetainės padės jums išmokti programuoti savo tempu.

kaip perkelti failus iš „Mac“ į „Android“
Skaityti toliau Susijusios temos
  • Programavimas
  • „JavaScript“
  • Python
  • C programavimas
Apie autorių Yuvraj Chandra(Paskelbti 60 straipsnių)

Yuvraj yra kompiuterių mokslo bakalauro studentas Delyje, Indijoje. Jis aistringas „Full Stack“ žiniatinklio kūrimui. Kai jis nerašo, jis tyrinėja skirtingų technologijų gylį.

Daugiau iš Yuvraj Chandra

Prenumeruokite mūsų naujienlaiškį

Prisijunkite prie mūsų naujienlaiškio, kad gautumėte techninių patarimų, apžvalgų, nemokamų el. Knygų ir išskirtinių pasiūlymų!

Norėdami užsiprenumeruoti, spustelėkite čia