This challenge is an extension of the previous Anagram challenge which was done on Day 3.(Link to Previous Challenge).
Using the .sort() method and .indexOf() method to solve this problem is not very efficient(Why?). We shall now redo the problem using a more efficient method of creating Objects.
- Write a function that accepts two strings and checks whether the first string is an anagram of the second string. If yes, the function returns true , else it returns false. The case(upper/lower)of the character MATTERS.
- You need to create an Object for each of the strings and then compare the two Objects for equality.
- The keys of the Object will be the characters in the string and the value will be the frequency of the character.
For eg."Stressed", "desSerts"
Object for "Stressed"
obj1={
'S':1,
't':1,
'r':1,
'e':2,
's':2,
'd':1
}
Object for "desSerts"
obj2={
'd':1,
'e':2,
's':2,
'S':1,
'r':1,
't':1
}
- You can compare the two Objects for equality by checking if they have the same keys and the same key-values.
Hint: You can get the list of the keys of the object using Object.keys()