-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.longest-palindromic-substring.cpp
56 lines (39 loc) · 1.19 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
/*
* @lc app=leetcode id=5 lang=cpp
*
* [5] Longest Palindromic Substring
*/
// @lc code=start
#include <iostream>
#include <utility>
using namespace std;
class Solution {
public:
pair<int, int> expandAroundCenter(string input, int j, int k) {
while (j >= 0 && k < input.size())
{
// not palindrome
if (input.at(j) != input.at(k))
{
break;
}
j--;
k++;
}
return make_pair(j + 1, k - 1);
}
string longestPalindrome(string input) {
if(input == "" || input.size() == 1) return input;
pair<int, int> longest = { 0, 0 };
for(int i = 0; i < input.size(); i++) {
pair<int, int> res;
auto res1 = expandAroundCenter(input, i, i);
auto res2 = expandAroundCenter(input, i, i + 1);
if(res1.second - res1.first >= res2.second - res2.first) res = res1;
else res = res2;
longest = res.second - res.first > longest.second - longest.first ? res : longest;
}
return input.substr(longest.first, longest.second - longest.first + 1);
}
};
// @lc code=end