# Write a program in JavaScript to check if two strings are anagrams of each other or not

Given two strings ‘a’ and string ‘b’, we have to check if they are anagrams of each other or not and return True/False. For example,

Input-1

String a= “india”
String b= “nidia”

Output

True

Explanation − Since the given string ‘b’ contains all the characters in the string ‘a’ thus we will return True.

Input-2

String a= “hackathon”
String b= “achcthoon”

Output

False

Explanation − Since the given string ‘b’ doesn’t have all the characters as string ‘a’ have thus we will return False.

## The approach used to solve this problem

In the given strings ‘a’ and ‘b’, we will check if they are of the same length and then we will sort the strings. If both the strings are equal, then return “True”; if not, then print “False”.

• Take Input two strings ‘a’ and ‘b’

• A function checkStringAnagrams(string a, string b) which will return true if they are anagram of each other otherwise false.

• Find the length of both strings and check if they are the same.

• Now sort both strings in lexicographically order and check if they are equal or not.

• Return true or false accordingly.

## Example

function checkStringsAnagram(a, b) {
let len1 = a.length;
let len2 = b.length;
if(len1 !== len2){
console.log('Invalid Input');
return
}
let str1 = a.split('').sort().join('');
let str2 = b.split('').sort().join('');
if(str1 === str2){
console.log("True");
} else {
console.log("False");
}
}
checkStringsAnagram("indian","ndiani")

## Output

Running the above code will generate the output as,

True

Since the string ‘indian’ has the same set of characters as in the other string ‘ndiani’, both are anagrams of each other, and hence, and we will return True.