fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool isvowel(char ch){
  4. return(ch=='a' || ch== 'e'||ch=='i' || ch== 'o'||ch=='u');
  5. }
  6. int main() {
  7. string s;
  8. // enter string length must be even
  9. //cin>>s;
  10. s = "abcd";
  11. int v1=0,c1=0,v2=0,c2=0; // v1 ->vowels for first half , c1 -> consonants for second half and so on;'
  12. int len = s.length();
  13. for(int i=0;i<len/2;i++)
  14. if(isvowel(s[i]))
  15. v1++;
  16. else
  17. c1++;
  18. for(int i=len/2;i<len;i++)
  19. if(isvowel(s[i]))
  20. v2++;
  21. else
  22. c2++;
  23.  
  24. int last_index_first_part = len-1;
  25. int last_index_second_part = len/2 - 1 ;
  26. int i = last_index_first_part; // i->points the last index of second part
  27. int j = last_index_second_part; // j-> points the last index of first part
  28.  
  29. int ans = 0;
  30.  
  31. if(v1>v2) ans++;
  32.  
  33. // we have to iterate i from last index of second part to 2nd last index of first part
  34.  
  35. while(i!=(last_index_second_part-1)){
  36. if(isvowel(s[i])){
  37. v1++;
  38. v2--;
  39. }
  40. else{
  41. c1++;
  42. c2--;
  43. }
  44.  
  45. if(isvowel(s[j])){
  46. v1--;
  47. v2++;
  48. }
  49. else{
  50. c1--;
  51. c2++;
  52. }
  53. if(v1>v2) ans++;
  54. i--;
  55. if(j==-1)
  56. j=len-1;
  57. else
  58. j--;
  59. }
  60. cout<<ans;
  61. }
  62.  
  63. /*
  64. abcd -->2
  65. bacd -->2
  66.  
  67. */
Success #stdin #stdout 0s 4448KB
stdin
Standard input is empty
stdout
2