forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
39 lines (29 loc) · 790 Bytes
/
main.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
/// Source : https://leetcode.com/problems/longest-palindromic-substring/
/// Author : liuyubobobo
/// Time : 2019-01-11
#include <iostream>
using namespace std;
/// Brute Force
/// Time Complexity: O(n^3)
/// Space Complexity: O(1)
class Solution {
public:
string longestPalindrome(string s) {
if(s == "") return s;
int n = s.size();
string res = s.substr(0, 1);
for(int i = 0; i < n; i ++)
for(int j = i + res.size(); j < n; j ++)
if(ok(s, i, j) && j - i + 1 > res.size())
res = s.substr(i, j - i + 1);
return res;
}
private:
bool ok(const string& s, int a, int b){
for(; a < b && s[a] == s[b]; a ++, b --);
return a >= b;
}
};
int main() {
return 0;
}