Anagram program in JavaScript - Free Web Development || UI Development || React JS || Angular

Latest

Free Web Development ||Free UI Development || React JS || HTML5 || CSS3 || JavaScript || Angular Learn Web Development || Frontend Developer || Full Stack Developer

1 / 3
Welcome to Shravan Ghanchi

Monday, March 10, 2025

Anagram program in JavaScript

Given two strings s1 and s2 consisting of lowercase characters, the task is to check whether the two given strings are anagrams of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different.

nput: s1 = “silent”  s2 = “lisent”
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.

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

Snow
Forest
Mountains
Snow
Forest
Mountains