forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
43 lines (34 loc) · 920 Bytes
/
Copy pathprogram.cpp
File metadata and controls
43 lines (34 loc) · 920 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
#include<iostream>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
if(s.empty() || s.length() < 1) return "";
int start = 0;
int end = 0;
for(int i=0;i<s.length();i++){
int len1 = MiddleExpand(s,i,i);
int len2 = MiddleExpand(s,i,i+1);
int len = max(len1,len2);
if(len > end - start){
start = i - ((len-1)/2);
end = i + (len/2);
}
}
return s.substr(start,end-start+1);
}
int MiddleExpand(string s,int left,int right){
if(s.empty() || left > right) return 0;
while(left >= 0 && right < s.length() && s.at(left)==s.at(right)){
left--;
right++;
}
return right-left-1;
}
};
int main(){
string s;
getline(cin,s);
Solution sol;
cout<<sol.longestPalindrome(s);
}