• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
August 13, 2024 |2.8K Views

JavaScript Program to Check if Two Strings are Anagrams

Description
Discussion

In this video, we will create a C++ Program on how to check if Two Strings are Anagrams or not.

What are anagrams?
Anagrams are words or phrases you spell by rearranging the letters of another word or phrase.

For examples:
String 1: aabc
String 2: abca
Output: True

String 1: act
String 2: cat
Output: True

String 1: below
String 2: elbow
Output: True

Here in this video, we cover three different methods to check it:
1. Sorting: Sort both strings. Compare the sorted strings
2. Hashing: In this method, two arrays are used. 
- Iterate through every character of both strings and increment the count of characters in the corresponding count arrays. 
- Compare count arrays. 
- If both the count arrays are the same, then it returns true.
3. Using one hash: In this method, we use only one count array instead of two. We can increment the value in the count array for characters in str1 and decrement for characters in str2. At last, if all count values are equal to 0, then the two strings are anagrams of each other. 

Apart from that, we will see the time and space complexity of all mentioned methods as well.

CPP program to check if 2 strings are anagram of each other or not:
https://www.geeksforgeeks.org/cpp-program-to-check-whether-two-strings-are-anagram-of-each-other/