Advertisement
GastonFontenla

Untitled

Sep 29th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int sherlockAndAnagrams(string s){
  6.     // Complete this function
  7.     map<string, int> mapa;
  8.    
  9.     for(int i=0; i<s.size(); i++)
  10.     {
  11.         string k;
  12.         for(int j=i; j<s.size(); j++)
  13.         {
  14.             k += s[j];
  15.             string w = k;
  16.             sort(w.begin(), w.end());
  17.             mapa[w]++;
  18.         }
  19.     }
  20.    
  21.     long long ans = 0;
  22.    
  23.     for(auto i:mapa)
  24.     {
  25.         ans += (i.second*(i.second-1))/2;
  26.     }
  27.    
  28.     return ans;
  29. }
  30.  
  31. int main() {
  32.     int q;
  33.     cin >> q;
  34.     for(int a0 = 0; a0 < q; a0++){
  35.         string s;
  36.         cin >> s;
  37.         int result = sherlockAndAnagrams(s);
  38.         cout << result << endl;
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement