Different Consecutive Characters Practice Problem in Strings - CodeChef

For the question mentioned above, I have written a Python code. Even though the logic looks correct to me, one of the given inputs is not working with my code. Can you help me correct my code?

cook your dish here

t=int(input())
for i in range(t):
n=int(input())
s=input()
for i in range(n-1):
c=0
if(s[i]==s[i+1]):
c+=1
print(c)

The statement c = 0 should be outside of the inner for loop. This is because you want to calculate the number of consecutive matching characters. But while you are looping through the string you are always setting the value of c to 0. Your program in this case will either print 0 or 1. Put the c = 0 outside the for loop to get the desired result.

Updated code

1 Like

@roumak
Got it.Thank you…