We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I took the same approach but the algorithm checks the difference in length of frequency of characters. I first count the frequency of each letter and put it in a multimap, with frequency as key and letter as value. I then double loop in descending order of frequency (which removes all other letters and then checks if the letters alternate), and only check frequencies that are less than or equal to 1 in difference. So for example:
24>g24>p23>h20>x18>z17>v
In this example I would check g->p, g->h, p->h, and z->v. It would also break from the loop early if the highest length match is greater than the sizes of the 2 letters being matched plus 2 (you could also skip any letter combination checks that had a combined frequency that was equal or less than the previously matched length). So if I had a match with g->h, I would skip p->h and then break from the loop and not check z->v, because any other combination will always be a lesser length.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
I took the same approach but the algorithm checks the difference in length of frequency of characters. I first count the frequency of each letter and put it in a multimap, with frequency as key and letter as value. I then double loop in descending order of frequency (which removes all other letters and then checks if the letters alternate), and only check frequencies that are less than or equal to 1 in difference. So for example:
In this example I would check g->p, g->h, p->h, and z->v. It would also break from the loop early if the highest length match is greater than the sizes of the 2 letters being matched plus 2 (you could also skip any letter combination checks that had a combined frequency that was equal or less than the previously matched length). So if I had a match with g->h, I would skip p->h and then break from the loop and not check z->v, because any other combination will always be a lesser length.