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 agree. The solution, though a very good one, is hardly readable. The overusage of #defines is really weird.
The brute force approach timed out for me even with correct answers, so I prefer the editorial approach. Roughly it goes like this
For each substring, create a new freq array(O(n2)). Create a map with the key = hash of freqarray. The hash has to be decent enough so that 'abb' and 'aab' do not cause collission. The value part is the number of substrings that have that same key.
Once the map is created, we need to just iterate it through once to find the number of substrings which have the same freq map.
Hope that was clear enough.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
I agree. The solution, though a very good one, is hardly readable. The overusage of #defines is really weird.
The brute force approach timed out for me even with correct answers, so I prefer the editorial approach. Roughly it goes like this
For each substring, create a new freq array(O(n2)). Create a map with the key = hash of freqarray. The hash has to be decent enough so that 'abb' and 'aab' do not cause collission. The value part is the number of substrings that have that same key.
Once the map is created, we need to just iterate it through once to find the number of substrings which have the same freq map.
Hope that was clear enough.