-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.longest-palindromic-substring.cpp
70 lines (68 loc) · 1.52 KB
/
5.longest-palindromic-substring.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=5 lang=cpp
*
* [5] Longest Palindromic Substring
*
* https://leetcode.com/problems/longest-palindromic-substring/description/
*
* algorithms
* Medium (26.83%)
* Total Accepted: 504.9K
* Total Submissions: 1.9M
* Testcase Example: '"babad"'
*
* Given a string s, find the longest palindromic substring in s. You may
* assume that the maximum length of s is 1000.
*
* Example 1:
*
*
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
*
* Example 2:
*
*
* Input: "cbbd"
* Output: "bb"
*
*
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
pair<int,int> res;
if(s.empty() || s.length()==1)
return s;
int n=s.length();
for(int i=0;i<n-1;i++){
auto o = expand_around(s,i,i);
auto e = expand_around(s,i,i+1);
auto curr = o.second > e.second ? o:e;
res = curr.second > res.second ? curr:res;
}
return s.substr(res.first,res.second);
}
pair<int,int> expand_around(string &s,int l, int r){
int n = s.length();
while(l >=0 && r <n){
if(s[l]!=s[r])
break;
l--;r++;
}
l++;r--;
if(s[l]==s[r])
return pair<int,int>(l,r-l+1);
else
return pair<int,int>(l,0);
}
};
// int main(){
// Solution sol;
// string s = "a";
// cout << sol.longestPalindrome(s) << endl;
// }