Passwd problem in practice

hey ,
i was trying out the password problem in the practice session( Here’s the link to the problem: https://www.codechef.com/submit/PASSWD)
Though i could get all the three default testcases right,my solution is still giving WA during runtime.I tried a lot to debug my code but still couldn’t find out where I went wrong.
I have added my code below (JAVA) ,please do check this out and let me know were I went wrong.

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
 public static void main (String[] args) throws java.lang.Exception
 {
  Scanner sc=new Scanner(System.in);
     int tc=sc.nextInt();
     sc.nextLine();
  while(tc!=0){
  String str =sc.nextLine();
  System.out.println("input "+str);
  int N=str.length();
  int lc=0;int uc=0;int dig=0;int sp=0;
  int flag=0;// 0 indicate correct pswd
  if(N<10)
     {flag=1;  }
  else
  {
      char[] arr=str.toCharArray();
  for(int i=1;i<N-1;i++)
  {
     if(arr[i]>='a' && arr[i]<='z')
     {
        lc++;
     }
     else if(arr[i]>='A' && arr[i]<='Z')
       {uc++;
     }
     else if(arr[i]>='0' && arr[i]<='9')
     {dig++;
  }
  else if(arr[i]=='@'||arr[i]=='#'||arr[i]=='%'||arr[i]=='&'||arr[i]=='?')
  {sp++;
  }
  else{
  flag=1;
      break;
  }
  }
  if((arr[0]>='a'&& arr[0]<='z')||(arr[N-1]>='a'&& arr[N-1]<='z'))
      {lc++;}
  }
  if(flag==0 && lc>0 && uc>0 && dig>0 && sp>0)
  {System.out.println("YES");
  }
  else{
      System.out.println("NO");
      }
      tc--;}
  }
 }