-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongPalindSubStr.cpp
More file actions
56 lines (52 loc) · 900 Bytes
/
LongPalindSubStr.cpp
File metadata and controls
56 lines (52 loc) · 900 Bytes
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
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s="abb";//cin>>s;
int n=s.size();
int indexStart=0,indexEnd=0,maxCnt=1;
//ODD Length Palindrome
for(int i=0;i<n-1;i++)
{
int st=i,ed=i;
while(st>=0 && ed<n)
{
if(s[st]==s[ed])
{
st--;ed++;
}
else
break;
}
int substrcnt=ed-st-1;
if(substrcnt>maxCnt)
{
maxCnt=substrcnt;
indexStart=++st;
indexEnd=--ed;
}
}
//EVEN Length Palindrome
for(int i=0;i<n-1;i++)
{
int st=i,ed=i+1;
while(st>=0 && ed<n)
{
if(s[st]==s[ed])
{
st--;ed++;
}
else
break;
}
int substrcnt=ed-st-1;
if(substrcnt>maxCnt)
{
maxCnt=substrcnt;
indexStart=++st;
indexEnd=--ed;
}
}
cout<<s.substr(indexStart,maxCnt);
return 0;
}