Please help me in problem SNAKPROC

Question: https://www.codechef.com/SNCKQL17/problems/SNAKPROC .This code works perfectly on my IDLE but shows wrong answer on codechef.Please let me know the changes needed:

import sys,os  
n=int(raw_input())  
for i in range(n):  
	counth=0  
	countt=0  
	l=int(raw_input())  
	a=raw_input()  
	a=a.replace(".","")  
	for j in range(len(a)):      
            if a[j]=="H":     
			counth+=1  
		if a[j]=="T":  
			countt+=1  
	if countt!=counth:   
		print "Invalid"  
	elif a.find("HH")!=-1 or a.find("TT")!=-1:  
		print "Invalid"  
	else:  
		print "Valid"

Hey! I think problem with your code is while handling this case:

14

T…H…TH…T…H

Answer should be Invalid while your solution gives valid.
I hope you find the mistake.

1 Like

I believe that your code is failing here-

Input
1
4
..TH

Output
Valid

Expected Output    
Invalid

Hi,
Your code doesn’t handle testcase: T…H.

Actually Buddy, you are just checking these 2 constraints.

  1. No. of heads should be equal to no. of tails
    2)Two consecutive heads and tails.

but what needed to be checked is the sequence H then T then H then T and so on.
you need to mention the condition that Tail shouldn’t be come in the begining
and Head shouldn’t be come at the end.
and another constraint that after a Head count there should be a Tail count.

My submission https://www.codechef.com/viewsolution/13647066

hope it helped.

Your logic seems to be different from mine so cant really comment. However i wanted to know why my code runs on my IDLE but got wrong answer in the contest.

yes i made the required changes… it works !! thanks for the help… https://www.codechef.com/viewsolution/13787380