Help me in solving SNAKPROC problem

My issue

My code

#include <iostream>
using namespace std;

int main() {
    int r;
    cin >> r;
    while (r--) {
        int l;
        cin >> l;
        string s;
        cin >> s; // Instead of a loop, directly read the whole string

        int count = 0;
        for (int i = 0; i < l; ++i) {
            if (count != 1 && s[i] == 'T') {
                cout << "Invalid" << endl;
                break;
            }
            if(count!=0 && s[i]=='H'){
                cout<<"Invalid"<<endl;
                break;
            }
            if (s[i] == 'H') {
                count += 1;
            } 
            if (s[i] == 'T') {
                count -= 1;
            }
        }
        
        
        if(count==0){
            cout<<"Valid"<<endl;
        }
    }
}


Problem Link: SNAKPROC Problem - CodeChef

@pheonixkabir
have corrected your code . There were few implementation mistakes.

#include <iostream>
using namespace std;

int main() {
    int r;
    cin >> r;
    while (r--) {
        int l;
        cin >> l;
        string s;
        cin >> s; // Instead of a loop, directly read the whole string

        int count = 0,tm=0;
        for (int i = 0; i < l; ++i) {
            if (count != 1 && s[i] == 'T') {
                tm=1;
                cout << "Invalid" << endl;
                break;
            }
            if(count!=0 && s[i]=='H'){
                tm=1;
                cout<<"Invalid"<<endl;
                break;
            }
            if (s[i] == 'H') {
                count += 1;
            } 
            if (s[i] == 'T') {
                count -= 1;
            }
        }
        
        if(!tm)
        {
        if(count==0){
            cout<<"Valid"<<endl;
        }
        else
        cout<<"Invalid"<<endl;
        }
    }
}