
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How to check if two Strings are anagrams of each other using C#?
- A Program to check if strings are rotations of each other or not?
- Check if strings are rotations of each other or not in Python
- Program to check strings are rotation of each other or not in Python
- C# program to determine if Two Words Are Anagrams of Each Other
- C Program to check if two strings are same or not
- Check if all levels of two trees are anagrams or not in Python
- Check if two strings are anagram of each other using C++
- Check if two strings are equal or not in Arduino
- Check whether two strings are anagram of each other in Python
- Java Program to check whether two Strings are an anagram or not.
- Check if bits in range L to R of two numbers are complement of each other or not in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Are the strings anagrams in JavaScript
- Program to check two strings are 0 or 1 edit distance away or not in Python
