Sherlock and anagrams hackerrank solution

Опубликовано: 26 Июль 2020
на канале: Kuldip Ghotane
4,377
81

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/017e...

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
   • Hackerrank Interview Preparation Kit