/******************************************************* * Copyright (C) 2015 Haotian Wu * * This file is the solution to the question: * https://www.hackerrank.com/challenges/sherlock-and-anagrams * * Redistribution and use in source and binary forms are permitted. *******************************************************/ #include #include #include #include #include #include #include #include using namespace std; int main() { // In naive approach, we search all left string (O(N^2)), all right string with the same length (O(N)), and compare the strings O(N). Overall time complexity O(N^4). // Instead, we can use hash! We hash all SORTED substrings and count their occurence. If it occurs K times, we have (K(K-1)/2) such pairs. int tt; scanf("%d",&tt); map mp; while (tt--) { mp.clear(); char str[101]; scanf("%s",str); int l = strlen(str); string s = str; for (int i=0;i :: iterator j=mp.begin();j!=mp.end();j++) sum += (j->second*(j->second-1))/2; printf("%d\n",sum); } return 0; }