forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpsas.cpp
37 lines (37 loc) · 795 Bytes
/
lpsas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
string Solution::longestPalindrome(string A) {
int n,start=0,maxlen=1,low,high,i;
n=A.length();
if(n<2)
return A;
string lps="";
for(i=1;i<n;i++)
{
low=i-1;
high=i;
while(low>=0 && high<n && A[low]==A[high])
{
if(high-low+1>maxlen)
{
start=low;
maxlen=high-low+1;
}
low--;
high++;
}
low=i-1;
high=i+1;
while(low>=0 && high<n && A[low]==A[high])
{
if(high-low+1>maxlen)
{
start=low;
maxlen=high-low+1;
}
low--;
high++;
}
}
for(i=start;i<start+maxlen;i++)
lps+=A[i];
return lps;
}