I have been trying to solve the TICKETS5 problem under Beginner Section.
I have written a code in Java which I tested and it was working fine.
But the Compiler of code chef Says wrong so I must be wrong somewhere, can somebody help me and point out the error.
Here is the code
import java.util.Scanner;
public class Tickets5 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n;
String code;
n = in.nextInt();
for(int i=0;i<n;i++){
code = in.next();
if(code.length()==2){
char first = code.charAt(0);
char second = code.charAt(1);
if(first!=second){
System.out.print("YES");}
else{
System.out.println("NO");
}
}
else if(code.length()>=2&& code.length()<=100){
char first = code.charAt(0);
char second = code.charAt(1);
double len = Math.ceil(code.length())/2;double check = 0;
for(int a =2;a<code.length();a++){
try{
if(first==code.charAt(a)&&second==code.charAt(a+1)){
check++;
}
}catch(StringIndexOutOfBoundsException we){
continue;
}
}
if(check==len-1){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
else{
System.out.println("NO");
continue;
}
}
in.close();
}
}