What is the issue in this code?
The question is Palindromic Queries.
The code which I have given shows TLE.
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.next();
str = str.substring(0,n);
int m = sc.nextInt();
for (int i = 0; i < m ; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
boolean[][] dp = new boolean[n][n];
if(count(str,l-1,r-1,dp)){
System.out.println(“YES”);
}
else{
System.out.println(“NO”);
}
}
}
public static boolean count(String str,int l,int r,boolean[][] dp) {
if(l==r){
return true;
}
if(l-r==1){
if(str.charAt(l)==str.charAt(r)){
return true;
}
else{
return false;
}
}
if(dp[l][r]!=false){
return true;
}
boolean ans = count(str,l+1,r-1,dp) && str.charAt(l)==str.charAt(r);
return dp[l][r]=ans;
}
}