Help me in solving XYSTRP problem

My issue

I can solve it with for loop with checking indexes but that will take O(n) but I want it to solve with better way O(1) giving error? anybody?

My code

# cook your dish here
def chefAndString(s):
    count=0
    for i in range(0,len(s)-1,2):
        if "xy" in s:
            count+=1
    return count
t=int(input())
while(t>0):
    s=input()
    print(chefAndString(s))
    t-=1

Learning course: Greedy Algorithms
Problem Link: Chef and String Practice Problem in Greedy Algorithms - CodeChef

@pixeal_meat
plzz refer my c++ code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string s;
	    cin>>s;
	    int cnt=0;
	    for(int i=1;i<s.size();i++)
	    {
	        if(s[i]!=s[i-1])
	        {
	            i++;
	            cnt++;
	        }
	    }
	    cout<<cnt<<endl;
	}
	return 0;
}