Why it is giving tle?

#include

#include<bits/stdc++.h>

using namespace std;

bool solve(string s,int a, int b){

int n=s.length();

bool dp[n+1][n+1];

for(int i=0;i<n;i++){
	dp[i][i]=true;
}

for(int i=0;i<n-1;i++){
	dp[i][i+1]=(s[i+1]==s[i]);
}

for(int i=0;i<n-2;i++){
	int k=0;
	for(int j=i+2;j<n;j++){
		dp[k][j]=(s[k]==s[j])&&dp[k+1][j-1];
	}
}

return dp[a-1][b-1];

}

int main () {

int len;

cin >> len;

string s;

cin >> s;

int n;

cin >> n;


while(n--){

	int a,b;

	cin >> a >> b;

	if(solve(s,a,b)==true) cout << "YES" << endl;

	else cout << "NO" << endl;

}

return 0;

}