Simple t.c o(n^2) and sc o(1) solution using 2 pointer


#1

#include<bits/stdc++.h>
string Solution::longestPalindrome(string A) {
int n=A.length();
int ans=1;
int n1,n2;
n1=0;
n2=0;
for(int i=0;i<n;i++){
int p1=i-1;
int p2=i+1;
while(p1!=-1&&p2!=n&&A[p1]==A[p2]){
if((p2-p1+1)>ans){
n2=p2;
n1=p1;
ans=p2-p1+1;
}
p1–;
p2++;
}

    p1=i-1;
    p2=i;
    while(p1!=-1&&p2!=n&&A[p1]==A[p2]){
        if((p2-p1+1)>ans){
            n2=p2;
            n1=p1;
            ans=p2-p1+1;
        }
        p1--;
        p2++;
    }
    
}

string s2=A.substr(n1,(n2-n1+1));
return s2;

}