sherlock and anagrams hackerrank solution c 2b 2b

Solutions on MaxInterview for sherlock and anagrams hackerrank solution c 2b 2b by the best coders in the world

showing results for - "sherlock and anagrams hackerrank solution c 2b 2b"
Timeo
11 Jan 2017
1// Score: 50
2
3#include <algorithm>
4#include <iostream>
5#include <map>
6#include <string>
7using namespace std;
8
9int main() {
10  int n;
11  cin >> n;
12  for (int t = 0; t < n; t++) {
13    string str;
14    cin >> str;
15
16    map<string, int> lib;
17
18    for (int i = 1; i < str.size(); i++) {
19      for (int j = 0; j < str.size() - i + 1; j++) {
20        string tmp = str.substr(j, i);
21        sort(tmp.begin(), tmp.end());
22
23        if (lib.find(tmp) != lib.end()) {
24          lib[tmp]++;
25        } else {
26          lib[tmp] = 1;
27        }
28      }
29    }
30
31    int ans = 0;
32    for (auto const &el : lib) {
33      ans += el.second * (el.second - 1) / 2;
34    }
35
36    cout << ans << endl;
37  }
38
39  return 0;
40}
similar questions