I am using IntelliJ as my platform.
The question can be found here:https:“www.codechef.com/problems/SNAKPROC.”
My Code:
import java.util.Scanner;
class SnakeCounter {
int l;
int h=-1;int t=-1;
int k=-1;
String arr[];
void accept()
{
int co=-1;
Scanner sc=new Scanner(System.in);
int T;
T=sc.nextInt();
sc.nextLine();
arr=new String[T];
while(T!=0)
{
l=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
int c=-1;
for(int i=0;i<l;i++)
{
if(s.charAt(i)=='H')
{
h=h+1;
if(c==1)
{
k=0;
break;
}
c=1;
}
if(s.charAt(i)=='T')
{
t=t+1;
if(c==0)
{
k=0;
break;
}
c=0;
}
}
if(k==0)
arr[++co]="Invalid";
else if(h!=t)
arr[++co]="Invalid";
else
arr[++co]="Valid";
T=T-1;
h=-1;t=-1;k=-1;c=-1;
}
}
void display()
{
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
public static void main(String args[])
{
SnakeCounter obj=new SnakeCounter();
obj.accept();
obj.display();
}
}
AND it is giving the correct output for the given input example.