-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest palindrone substring.cpp
More file actions
106 lines (103 loc) · 3.2 KB
/
longest palindrone substring.cpp
File metadata and controls
106 lines (103 loc) · 3.2 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
int palindrones[1001][1001];
int dp[1001];
class Solution {
int _max = 0;
int start = 0, end = 0;
public:
bool is_palindrone(string& s, int i, int j) {
if (i >= j) return true;
if (palindrones[i][j] != -1) return palindrones[i][j];
if (s[i] == s[j]) {
return palindrones[i][j] = is_palindrone(s, i + 1, j - 1);
}
else {
return palindrones[i][j] = false;
}
}
int solve(string& s, int i, int j) {
if (i == j) {
return 0;
}
if (dp[i] != -1) {
return dp[i];
}
if (is_palindrone(s, i, j)) {
if (j - i + 1 > _max){
start = i;
end = j;
_max = j - i + 1;
}
return 0;
}
int ans = INT_MAX;
for (int k = i; k < j; k++) {
if (is_palindrone(s, i, k)) {
int temp = 1 + solve(s, k + 1, j);
ans = min(temp, ans);
if (k - i + 1 > _max) {
_max = k - i + 1;
start = i;
end = k;
}
}
}
return dp[i] = ans;
}
string longestPalindrome(string s) {
if (s.size() == 1){
return s;
}
memset(palindrones, -1, sizeof(palindrones));
memset(dp, -1, sizeof(dp));
int res = solve(s, 0, s.size() - 1);
string longest_palindrone = s.substr(start, _max);
return longest_palindrone;
}
};
//time: O(n^2) and space: O(n^2)
//space optimized solution
//Algorithm: Palindrone cane be made with odd number of character or even number.
// the idea is to move to left and right from center and check if it's palindrone or not.
// time: O(n^2) and space: O(1)
class Solution {
public:
pair<int, int> expand_around_center(string& s, int mid, int left, int right) {
int n = s.size();
int _max = INT_MIN;
int start = left;
int end = right;
while (left >= 0 && right < n && s[left] == s[right]) {
if (right - left + 1 > _max) {
start = left;
end = right;
// updating new langht of palindrone
_max = right - left + 1;
}
left--;
right++;
}
return {start, _max};
}
string longestPalindrome(string s) {
int n = s.size();
int start = 0;
int _max = 1;
// checking for odd number palindrone
for (int mid = 0; mid < n; mid++) {
// checking for odd number palindrone
pair<int, int> odd_palindrone = expand_around_center(s, mid, mid, mid);
// checking for even number palindrone
pair<int, int> even_palindrone = expand_around_center(s, mid, mid, mid + 1);
if (odd_palindrone.second > _max) {
_max = odd_palindrone.second;
start = odd_palindrone.first;
}
if (even_palindrone.second> _max) {
_max = even_palindrone.second;
start = even_palindrone.first;
}
}
string result = s.substr(start, _max);
return result;
}
};