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.
intsherlockAndAnagrams(strings){intn=s.size();intanagram_count;for(inti=1;i<n;i++){// i for size of substringsunordered_map<string,int>store;stringsubstring;for(intj=0;j<n-i+1;j++){// j for the starting position of the substringsubstring=s.substr(j,i);// substring starting at position j of length isort(substring.begin(),substring.end());// sort the substringstore[substring]++;//count the number of times substring appears, up to rearrangement}for(autot:store){if(t.second>1){anagram_count+=(t.second)*(t.second-1)/2;// number of pairs is n choose 2 if n appears more than twice}}}returnanagram_count;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
C++20 :