Program to check if two strings are anagrams or not is discussed here. Two strings are given as input and those strings have to be checked if they are anagrams or not. Anagram means that both strings contain the same character set, only their order is different. Therefore, in both strings, the frequency of each letter must be the same. For example, strings "act" and "cat" are anagrams.
The solution to this problem can be given in two different ways.
Method 1: Count the frequency of alphabets in both the strings and store them in respective arrays. If the two arrays are equal, return true. Else, return false.
Method 2: Sort both the strings and compare if both the sorted strings are equal. If they are equal, return true. Else, return false
Output : The strings are anagrams
Time complexity: O(n)
String 2 : act
The two strings are anagram of each other