Explanation of the String Anagram Problem
String a= “hello” String b= “llheo”
String a= “hackathon” String b= “achcthoon”
Explanation: Since the given string b doesn’t contain all the characters of string a so we will return false.
Approach to solve this problem
First of all, we will check whether the length of both the strings a and b are of the same length or not. If the length of both strings is not the same then we will simply return false. If it is the same then we will proceed further.
- Take two input strings 1 and string 2.
- Create a function to check whether the strings are anagram or not. In our case, we have taken isAnagram as the function and it will return true if the strings are anagram and will return false if they are not.
- We will just count the frequency of every element of string 1 and iterate through string2 and decrease the count of every element if they are similar to string 1. If at any point the frequency is not equal to 0 then the strings are not an anagram of each other.
Time Complexity: O(n) where n is the length of the string
Space Complexity: O(1)