-
Notifications
You must be signed in to change notification settings - Fork 7
/
LPS.cpp
44 lines (40 loc) · 1.07 KB
/
LPS.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
38
39
40
41
42
43
44
//Longest palindromic substring in O(n^2) time and O(1) space
#include <iostream>
#include <string.h>
using namespace std;
int longPalSubstr(char str[]) {
int n = strlen(str);
int i,low,high,len=1,start=0,maxlen=1;
for(i=1;i<n;i++) {
//find even length substrings centred at i-1 and i
low=i-1; high=i;
while(low>=0 && high<n && str[low]==str[high]) {
len = high-low+1;
low--; high++;
}
if(len>maxlen) {
maxlen = len;
start = i - len/2;
}
//find odd length substring centred at i
low=i-1; high=i+1;
while(low>=0 && high<n && str[low]==str[high]) {
len = high-low+1;
low--; high++;
}
if(len>maxlen) {
maxlen = len;
start = i - len/2;
}
}
cout<<"Longest palindromic substring : ";
for(i=start;i<start+maxlen;i++)
cout<<str[i];
return maxlen;
}
int main() {
char str[20];
cin>>str;
cout<<"\nLength : "<<longPalSubstr(str)<<"\n";
return 0;
}