-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.longest-palindromic-substring.cpp
More file actions
55 lines (49 loc) · 1.63 KB
/
5.longest-palindromic-substring.cpp
File metadata and controls
55 lines (49 loc) · 1.63 KB
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
class Solution {
public:
int getPalindromeSize(const std::pair<int, int>& palindrome_bounds) {
return palindrome_bounds.second - palindrome_bounds.first + 1;
}
std::pair<int, int> expandOutwardOdd(int center, const std::string& s) {
int left = center - 1;
int right = center + 1;
while (left >= 0 && right < s.size() && s[left] == s[right]) {
--left;
++right;
}
return std::make_pair(left + 1, right - 1);
}
std::pair<int, int> expandOutwardEven(int center_left, int center_right,
const std::string& s) {
int left = center_left - 1;
int right = center_right + 1;
while (left >= 0 && right < s.size() && s[left] == s[right]) {
--left;
++right;
}
return std::make_pair(left + 1, right - 1);
}
string longestPalindrome(string s) {
std::cout << "s: " << s << std::endl;
std::cout << "size: " << s.size() << std::endl;
if (!s.size()) {
std::cout << "here" << std::endl;
return "";
}
std::pair<int, int> best_p({0, 0});
for (int i = 0; i < s.size(); ++i) {
std::pair<int, int> current_odd_p = expandOutwardOdd(i, s);
if (getPalindromeSize(best_p) < getPalindromeSize(current_odd_p)) {
best_p = current_odd_p;
}
}
for (int i = 0; i < s.size() - 1; ++i) {
if (s[i] == s[i + 1]) {
std::pair<int, int> current_even_p = expandOutwardEven(i, i + 1, s);
if (getPalindromeSize(best_p) < getPalindromeSize(current_even_p)) {
best_p = current_even_p;
}
}
}
return s.substr(best_p.first, best_p.second - best_p.first + 1);
}
};