Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Two strings are anagrams if they contain the same characters with the same frequency, just rearranged.
Input-1 ?
String a = "india" String b = "nidia"
Output ?
True
Explanation ? Since the given string 'b' contains all the characters in the string 'a' with the same frequency, we will return True.
Input-2 ?
String a = "hackathon" String b = "achcthoon"
Output ?
False
Explanation ? Since the given string 'b' doesn't have the same character frequencies as string 'a', 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 sorted 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 lexicographical order and check if they are equal or not.
Return true or false accordingly.
Method 1: Using Sorting Approach
function checkStringsAnagram(a, b) {
let len1 = a.length;
let len2 = b.length;
if(len1 !== len2){
console.log('False - Different lengths');
return false;
}
let str1 = a.split('').sort().join('');
let str2 = b.split('').sort().join('');
if(str1 === str2){
console.log("True");
return true;
} else {
console.log("False");
return false;
}
}
// Test cases
checkStringsAnagram("indian", "ndiani");
checkStringsAnagram("listen", "silent");
checkStringsAnagram("hello", "bello");
True True False
Method 2: Using Character Frequency Count
function checkAnagramsFrequency(str1, str2) {
if (str1.length !== str2.length) {
console.log("False - Different lengths");
return false;
}
const charCount = {};
// Count characters in first string
for (let char of str1) {
charCount[char] = (charCount[char] || 0) + 1;
}
// Subtract character counts for second string
for (let char of str2) {
if (!charCount[char]) {
console.log("False - Character mismatch");
return false;
}
charCount[char]--;
}
console.log("True");
return true;
}
// Test cases
checkAnagramsFrequency("race", "care");
checkAnagramsFrequency("rat", "car");
True False - Character mismatch
Comparison
| Method | Time Complexity | Space Complexity | Advantage |
|---|---|---|---|
| Sorting | O(n log n) | O(n) | Simple implementation |
| Frequency Count | O(n) | O(1) | More efficient |
Conclusion
Both methods effectively check for anagrams. The sorting approach is simpler to understand, while the frequency counting method is more efficient with O(n) time complexity. Choose based on your performance requirements.
