Hi I am a beginner in C++. I was trying to solve the Longest palindrome challenge and my code it able to correctly solve some of the cases and all the input examples. However it giving a WA for some others. Could you please help me find the answer.
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string w){
int length = w.size();
// cout << length;
for(int i = 0; i < length/2; i++){
if(w.at(i) != w.at(length - (i + 1))){
return false;
}
}
return true;
}
int main() {
// cout << "Started";
int N = 0;
cin >> N;
string word;
cin >> word;
int length = word.size();
int maxLength = 0;
string longestP;
for(int i = 0; i <= length; i++){
for(int j = i + 1; j <= length; j++){
string curr = word.substr(i,j);
int length = curr.size();
//cout << length;
if(isPalindrome(curr)){
string p = curr;
//cout << p << " ";
int currLength = p.size();
if(currLength > maxLength){
maxLength = currLength;
longestP = p;
}
}
}
}
cout << maxLength << "\n" << longestP;
return 0;
}