What is the error in this code?

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();
}
}

also should ABA and ABABAC give YES?

Try this test case-

Input
1
ABABA
Expected Output
YES
Your Output
NO

For seeing what is causing the problem, i recommend you manually see how this snippet of your code works-

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;
                }

You will see that the condition if(check==len-1){ will not evaluate to true, hence giving WA.

ABA=Yes, ABABAC=NO

why should ABABA should give yes? Its said alternate pairs right?

Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...".

Or, x is followed by y and y is followed by x for entire string length. So “pairs” is not important. “xyx” “xyxyxyxy” are all alternating.

oh okay, so ABABA is correct and ABABC is not. I think I understood what needs to be done, thanks

//