How to Check if Two Strings Are Anagram

An anagram is a literary device. In this device, two words are called anagrams of each other if the alphabets of one of the words are rearranged letters of the other word. Hence, the list of letters of both words should be the same. For example, the string 'abcde' and the string 'bdeac' are anagrams of each other. These are anagrams because the list of letters in both strings is the same, i.e., ['a', 'b', 'c', 'd', 'e']. In this problem, we are given two strings. Our task is to tell if the two strings are anagrams or not. Let us see some examples to understand the problem.

Input: s1 = "listen" s2 = "silent"

Output: Anagram

Explanation: The list of characters of the words are the same, i.e., ['l', 'i', 'e', 'n', 't', 's'].

Input: s1 = "gram" s2 = "arm"

Output: Not an anagram

Explanation: The list of characters of the word "gram" is ['g', 'r', 'a', 'm'] and the list of characters of the word "arm" is ['r', 'a', 'm']. Since the list is not the same, therefore, the two words are not anagrams.

Approach - 1

In this approach, we will use a basic approach to solve the problem. We will sort the strings, and if the sorted strings are equal, then the strings are anagrams.

Here is the algorithm of this approach:

  • We will sort the two given strings
  • Then, we will compare the sorted strings.
  • If the sorted strings are equal, then we will return a True
  • Else, we will return False

Below is the implementation of this approach in Python.

Code

Output:

The given two strings are not anagrams

Time Complexity: This approach has logarithmic time complexity. To perform sorting, the time complexity is O(log N).

Space Complexity: We have not used any extra space; hence, the space complexity is O(1).

Approach - 2

In this approach, we will check if the strings are anagrams or not by counting the frequency of letters.

We will count the frequency of each character of the two strings, and if the strings contain same characters and the frequency of each character matches, then the given strings are anagrams.

Let us see the algorithm of this approach:

  • We will initialize two arrays of length equal to the total number of characters possibly the strings can have, which is equal to 256. We will initialize all values of this array as 0.
  • We will iterate over both the strings one by one and update the frequency of the characters in the corresponding arrays.
  • Then, we will compare the count arrays. We will compare the count frequencies of characters of both strings, and if all the frequencies are the same, then the strings are anagrams, and if the frequency of any one of the characters is different, then the strings are not anagrams.

Below is the implementation of the above-mentioned approach.

Code

Time Complexity: This approach has logarithmic time complexity. To perform sorting, the time complexity is O(log N).

Space Complexity: We have not used any extra space; hence, the space complexity is O(1).

Approach - 2

In this approach, we will check if the strings are anagrams or not by counting the frequency of letters.

We will count the frequency of each character of the two strings, and if the strings contain same characters and the frequency of each character matches, then the given strings are anagrams.

Let us see the algorithm of this approach:

  • We will initialize two arrays of length equal to the total number of characters possibly the strings can have, which is equal to 256. We will initialize all values of this array as 0.
  • We will iterate over both the strings one by one and update the frequency of the characters in the corresponding arrays.
  • Then, we will compare the count arrays. We will compare the count frequencies of characters of both strings, and if all the frequencies are the same, then the strings are anagrams, and if the frequency of any one of the characters is different, then the strings are not anagrams.

Below is the implementation of the above-mentioned approach.

Code

Output:

The given two strings are not anagrams

Time Complexity: We have used linear loops to solve this problem hence the time complexity is O(n)

Auxiliary Space: We have created two arrays to store the frequency of the letters, so the space complexity is O(N), where N is the total count of characters, which is 256.

Approach - 3

In this approach, we will check if the given two strings are anagrams of each other or not using HashSet.

This approach is the optimized solution of the above approach. We do not need to create the count arrays of length 256. We can use a hashset to store the frequencies of each character.

In this approach, we will not create two hash sets; instead, we will use only one HashSet. We will insert the frequency of characters of one string. Then, we will iterate over the other string and decrease the frequency of the characters. If, at last, the frequency of all the characters is zero, then the given strings are anagrams of each other. If this is not true then the strings are not anagrams.

Here is the algorithm of this approach:

  • We will create a hashset.
  • Then, we will iterate over one of the strings and store the frequency in the HashSet.
  • Then, we will iterate over the other string and check
  • If the character is in HashSet, then decrease the frequency
  • Otherwise, return false as the character sets do not match.
  • At last, we will check if the frequency of each key is zero or not.
    • If not zero, then return False.
  • At last, return True.

Below is the implementation of the above-mentioned approach.

Code

Output:

The given two strings are not anagrams

Time Complexity: The time complexity of this approach is linear because we have used linear loops to solve the problem. Hence, the time complexity is O(N).

Space Complexity: The space complexity of this program is less as compared to the previous program. In this approach, we have created only one hashset of length N. Therefore, the space complexity is O(N).