Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active May 10, 2021 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/ab25cbc54a1d6c25215dad20b2f76c76 to your computer and use it in GitHub Desktop.
Save McLarenCollege/ab25cbc54a1d6c25215dad20b2f76c76 to your computer and use it in GitHub Desktop.
Anagram Part 2

Practice Challenge #6

Anagram Part 2

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.

Problem Statement

  • 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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment