nput: s1 = “silent” s2 = “lisent”
Output: true
Explanation: Both the string have same characters with same frequency. So, they are anagrams.
Output: true
Explanation: Both the string have same characters with same frequency. So, they are anagrams.
Input: s1 = “foo” s2 = “bar”
Output: false
Explanation: Characters in both the strings are not same.
Output: false
Explanation: Characters in both the strings are not same.
Javascript Program :
---------------------------------
const isAnnagram=(str1,str2)=>{
if(str1.length!=str2.length)
{
return false;
}
const lowerStr1 = str1.toLowerCase();
const lowerStr2 =str2.toLowerCase();
if(lowerStr1===lowerStr2){
return false;
}
const sortedSort1=lowerStr1.split("").sort().join("");
const sortedSort2=lowerStr2.split("").sort().join("");
return sortedSort1===sortedSort2;
}
isAnnagram("foo","bar"); //false
isAnnagram("silent","lisent") //true
----------------------
No comments:
Post a Comment