My issue
a=int(input())
for b in range(a):
c=str(input())#‘xyxxy’
d=list(c)#[x,x,y,y]
e=0
for i in range(len(d)-1):
if d[i]==‘x’ and d[i+1]==‘y’ or d[i]==‘y’ and d[i+1]==‘x’:
e=e+1
for j in range(len(d)-2):
if d[j]==‘x’ and d[j+1]==‘y’ and d[j+2]==‘x’:
e=e-1
if e>0:
print(e)
else:
print(0)
#please find error, on submission its showing wrong answer
My code
a=int(input())
for b in range(a):
c=str(input())
d=list(c)
e=0
for i in range(len(d)-1):
if d[i]=='x' and d[i+1]=='y' or d[i]=='y' and d[i+1]=='x':
e=e+1
for j in range(len(d)-2):
if d[j]=='x' and d[j+1]=='y' and d[j+2]=='x':
e=e-1
if e>0:
print(e)
else:
print(0)
Learning course: Greedy Algorithms
Problem Link: Chef and String Practice Problem in Greedy Algorithms - CodeChef