Help me in solving LARGESUB problem

My issue

where my code get failed

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
     Scanner sc=new Scanner(System.in);
     int t=sc.nextInt();
     for(int k=0;k<t;k++)
     {
         long n=sc.nextLong();
         String s=sc.next();
         long a=0;
         long b=0;
         for(int i=0;i<n-1;i++)
         {
            if(s.charAt(i)==s.charAt(i+1))
            {
                if( s.charAt(i)=='a')
                {
                a++;
                }
                else{
                    b++;
                }
            }
         } 
         if(s.charAt(0)==s.charAt((int)n-1) || a+1==(int)n || b+1==(int)n)
         {
             System.out.println(n);
         }
         else 
         {
             System.out.println(n-Math.min(a+1,b+1));
         }
     }
     sc.close();
	}
}

Problem Link: Largest Subsequence Practice Coding Problem