Checking for string anagrams JavaScript


In this problem statement, our aim is to check for string anagrams with the help of Javascript functionalities. So for solving this problem first we need to understand the problem in simple terms.

Understanding the problem statement

We have given a string as an input string and our main aim is to check if the string is an anagram string or not. If it is anagram then return true otherwise return false.

What do you mean by anagrams?

In the given problem statement there is the usage of the word anagram!! Let’s first understand the meaning of this word. Anagram is a word which is formed by rearranging the letters of any word or sentence. Basically we can say that if two words have similar letters with the same length it is called anagrams. For example we have a string “silent”, if we rearrange this string and after rearranging - “listen”, so silent and listen have similar letters and have the same length 6. So this is known as string anagrams.

Logic for the given problem

For the code we will first clean the given strings as the string can also contain non word characters and also uppercase letter or regular expressions so clean and remove them from the string. After that we will split the strings into an array of characters. Then we will sort them with the help of the sort method in Javascript. So now we will have sorted characters and then join them again into the string. At the end we will compare the sorted strings to check if they are equal or not. If they are similar then they are anagrams.

Algorithm

Step 1 − Declare a function called isAnagram which is using two string arguments.

Step 2 − Convert the given string in lower case and remove any non characters from the input strings to get the required and actual result.

Step 3 − Check both the strings after removing extra characters after applying split, sort and join methods. This line of code will get the output in boolean form.

Step 4 − Return the result as true or false.

Code for the algorithm

//function to find out anagram status
function isAnagram(str1, str2) {
   // Remove any non-word characters and convert to lowercase
   const remove1 = str1.replace(/[^\w]/g, "").toLowerCase();
   const remove2 = str2.replace(/[^\w]/g, "").toLowerCase();
 
   // Check if the sorted strings are equal
   return remove1.split("").sort().join("") === remove2.split("").sort().join("");
}
const anagram1 = isAnagram("tutorials", "uttoslair");
const anagram2 = isAnagram("point", "heoll");
// example usage
console.log(anagram1);
console.log(anagram2);

Complexity

The time taken by the function is O(n log n) because the method uses the sort method of Javascript to sort the string characters. And n is the size of the string. And the space used by the code is O(n) as it is storing the new array of the cleaned and sorted array..

Conclusion

So the above created function can be used to find out if the given strings are anagram or not with the time complexity O(n log n). We have basically used some built-in methods to solve the given problem.

Updated on: 18-May-2023

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements