Back to Browse

Sherlock and anagrams hackerrank solution

4.6K views
Jul 26, 2020
29:40

In this problem sherlock and anagram from hackerrank interview preparation kit series I have used map to solve this problem code: https://gist.github.com/Kuldip-G/017e2add501c18240d0649b0a6c3bb0c Naive approach : check for no of substrings present then sort them add map them to our unordered_map using key value of string, int (key,value) pair. In this approach we can use sorting to ensure all the pairs are categorized in the same key Optimized : In above part instead of sorting every time we can create a distinct value for each string, this can be done by assigning a prime value to every char (a-2.b-3...) and then multiply them( ab will be 6 and ba as well) hence it will help us create a key value pair of int ,int . then we can calculate no of distinct pair of substrings using n(n-1)/2 where n is length of string. Thanks for stopping by If you are solving problem from hackerrank interview preparation kit series and need help or want to check for optimized solution do check this playlist https://www.youtube.com/playlist?list=PL_7PgBhpX0WbdJdiKskPkBflqLvCEtsOE

Download

1 formats

Video Formats

360pmp439.1 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

Sherlock and anagrams hackerrank solution | NatokHD