-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.cpp
More file actions
36 lines (32 loc) · 828 Bytes
/
Copy path5.cpp
File metadata and controls
36 lines (32 loc) · 828 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
#include <string>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int idx { 0 };
int len { 0 };
for (int i { 0 }; i < s.size(); i++) {
int l { i };
int r { i };
while (l >= 0 && r < s.size() && s[l] == s[r]) {
if (r - l + 1 > len) {
idx = l;
len = r - l + 1;
}
l--;
r++;
}
l = i;
r = i + 1;
while (l >= 0 && r < s.size() && s[l] == s[r]) {
if (r - l + 1 > len) {
idx = l;
len = r - l + 1;
}
l--;
r++;
}
}
return s.substr(idx, len);
}
};