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"

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.

Actually Buddy, you are just checking these 2 constraints.

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.