You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* A simple method which first compares the lengths of strings and then iterates through the characters of any string and check whether it exists in the other one as well and does same for the other string
* Please note that this is not at all an efficient method. Do not use this.
*/
functionanagram(str1,str2){
letlen1=str1.length,
len2=str2.length;
// Lengths of both strings must be same
if(len1!==len2){
console.log('Invalid Input');
return-1;
}
// check characters of string 1 are there in string 2
letflag=1;
for(letcharofstr1){
if(str2.indexOf(char)<0){
flag=0;
break;
}
}
if(flag!==1){
console.log(`${str1} and ${str2} are not Anagrams`);
return0;
}
for(letcharofstr2){
if(str1.indexOf(char)<0){
flag=0;
break;
}
}
if(flag!==1){
console.log(`${str1} and ${str2} are not Anagrams`);