# How to Solve String Anagram Problem in Javascript | String Anagram Problem in Javascript By Technical Suneja

The string anagram problem is one of the most asked questions in a javascript interview to test your data structure knowledge. This is a very common question that you will come across while practicing algorithms.

##### Explanation of the String Anagram Problem

The gist of the string anagram problem in javascript is you will be given two strings ‘a’ and ‘b’. You need to check whether all the characters of string a are similar to the characters of string b or not. If they are similar and the length of the string is the same then we have to return true or else we have to return false.

##### For Example:

Input 1:

```String a= “hello”
String b= “llheo”```

Output 1:

`True`

Input 2:

```String a= “hackathon”
String b= “achcthoon”```

Output 2:

`False`

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.
##### Javascript Code : Output: True

Time Complexity: O(n) where n is the length of the string

Space Complexity: O(1)

### How To Handle 90 Days Notice Period With HR

#### Stay Connected

0Fans
3,742Followers
19,400Subscribers