fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. // 4th Sep Amazon SDE 1
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL);
  12. string s;
  13. cin >> s;
  14. unordered_map<char, int> freq;
  15. map<vector<int>, int> mp;
  16. int ans = 0;
  17. mp[{0, 0, 0}] = 1;
  18. for (auto it : s)
  19. {
  20. freq[it]++;
  21. int x1 = freq['a'] - freq['b'];
  22. int x2 = freq['c'] - freq['d'];
  23. int x3 = freq['u'] - freq['t'];
  24. ans += mp[{x1, x2, x3}];
  25. mp[{x1, x2, x3}]++;
  26. }
  27. cout << ans << endl;
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5304KB
stdin
abbacdutt
stdout
10