#include <iostream>
#include <cmath>
// 3
// xy 1 1
// xyxxy 3 2
// yy 0 2
int main()
{
int t;
std::cin >> t;
while(t--)
{
std::string s;
std::cin >> s;
int count1=0 ;
int count2=0;
for (int i=0;i<s.length();++i)
{
if(s[i]=='x')
++count1;
else
++count2;
}
if(count1==0 || count2==0)
{
std::cout << 0 <<'\n';
}
else
{
std::cout << floor((count2+count1)/2) << '\n';
}
}
return 0;
}
Your logic seems to be completely wrong. You cannot depend on the count of x and y. You need to take care of their positions too.
If and only if x is adjacent to y or vice-versa, you increase the pair count by 1.
AC code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
long long p = 0;
for (int i = 0; i < s.size()-1; ++i)
{
if ((s[i] == 'x' && s[i+1] == 'y') || (s[i] == 'y' && s[i+1] == 'x'))
{
p += 1;
++i;
}
}
cout << p << '\n';
}
}
Thanks !