Problem Statement:(👈click here for GFG)
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram 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. For example, act and tac are an anagram of each other.
Note:-
If the strings are anagrams you have to return True or else return False
|s| represents the length of string s.
Example 1:
Input:a = geeksforgeeks, b = forgeeksgeeks
Output: YES
Explanation: Both the string have same characters with
same frequency. So, both are anagrams.
Example 2:
Input:a = allergy, b = allergic
Output: NO
Explanation: Characters in both the strings are
not same, so they are not anagrams.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isAnagram() which takes the string a and string b as input parameter and check if the two strings are an anagram of each other. The function returns true if the strings are anagram else it returns false.
Expected Time Complexity:O(|a|+|b|).
Expected Auxiliary Space: O(Number of distinct characters).
Constraints:
1 ≤ |a|,|b| ≤ 10^5
Solution:
class Solution {
isAnagram(a, b) {
if (a.length !== b.length) {
return false;
}
const freq = new Array(26).fill(0);
for (let i = 0; i < a.length; i++) {
freq[a.charCodeAt(i) - 'a'.charCodeAt(0)]++;
}
for (let i = 0; i < b.length; i++) {
freq[b.charCodeAt(i) - 'a'.charCodeAt(0)]--;
if (freq[b.charCodeAt(i) - 'a'.charCodeAt(0)] < 0) {
return false;
}
}
return true;
}
}
Comments
Post a Comment