fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // your code goes here
  8. vector<char>arr = {'x','x','z','y','z','y','y','z'};
  9. int count = 0 ;
  10. map<pair<int,int>,int>hash;
  11. hash[{0,0}]=1;
  12. int n = arr.size();
  13. int x_count,y_count,z_count;
  14. x_count = y_count=z_count=0;
  15. for(int i =0;i<n;i++){
  16. if(arr[i]=='x') x_count++;
  17. else if(arr[i]=='y') y_count++;
  18. else {
  19. z_count++;
  20. }
  21. count+= hash[{x_count-y_count,y_count-z_count}];
  22. hash[{x_count-y_count,y_count-z_count}]++;
  23.  
  24. }
  25. cout<<count;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
2