

Now that we’ve done our analysis, it’s showtime! 🎆 These parts could be just a letter or a pair of letters, such as what have we seen in the example above - “ m” or “ mo.” In our solution, we will split the original string to such substrings and then we will go over them and do the comparison, which will tell us whether we have anagrammatic pairs among them. Substrings, as the name infers, are parts of a string. In order to do that, we need to split this original string to substrings. We will have to count how many pairs like this can be found in a given string. Such as “mo” and “om”, or “listen” and “silent”. Since we’ve seen what an anagram is, it should be relatively easy to conclude that an anagrammatic pair is just two strings that are anagrams. So we will have to look for words and compare them with other words, in order to see if they are anagrammatic pairs. As it is described above, an anagram of one word is another word that has the same length and is created with the same characters from the former word.Īnimation for the anagram “Listen = Silent” Since we are going to look for anagrams, let’s start with them. You’ll probably think that you should use this kind of data structure when solving it. I think this is a good moment to mention that the challenge in question is under the “Dictionaries and Hashmaps” section in the HackerRank website. In other words, we need to have a clear picture of what are we trying to solve, before solving it.įrom the description of the problem, we can deduct all we need. What is an anagram? What is an anagrammatic pair? Can I see one? Also, what exactly does it mean substrings? Analysisįirst thing first - we need to get a better understanding of the whole problem. Given a string, find the number of pairs of substrings of the string that are anagrams of each other.įor example s = mom, the list of all anagrammatic pairs is, at positions, ],, ] respectively.Ĭonstraints Length of the input string: 2 ≤ |s| ≤ 100 String s contains only lowercase letters from the range ascii. Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. If you want to give a try yourself, please stop here and go to HackerRank’s site. ⚠️CAUTION: I will roll out my solution below with short explanations about each of the steps. That’s why I decided to write an article about it and try to put some nice and easy to digest explanations along the way.


Also, any explanations were completely out of the question. I found just one, and it was not working correctly. When I tried to google it, I could not find a decent JS solution. I spent a lot of time trying to solve it, with JavaScript. This post is going to get you through my solution to a coding challenge called “Sherlock and Anagrams.” You may take a look at it in HackerRank.
