forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestPalindromicSubstring.cpp
199 lines (163 loc) · 6.71 KB
/
longestPalindromicSubstring.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Source : https://oj.leetcode.com/problems/longest-palindromic-substring/
// Author : Hao Chen
// Date : 2014-07-17
/**********************************************************************************
*
* Given a string S, find the longest palindromic substring in S.
* You may assume that the maximum length of S is 1000,
* and there exists one unique longest palindromic substring.
*
**********************************************************************************/
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string findPalindrome(string s, int left, int right)
{
int n = s.size();
int l = left;
int r = right;
while (left>=0 && right<=n-1 && s[left] == s[right]) {
left--;
right++;
}
return s.substr(left+1, right-left-1);
}
// This is the common solution.
// Actuatlly it's faster than DP solution under Leetcode's test
// the below optimized DP solution need 700ms+, this needs around 250ms.
string longestPalindrome_recursive_way(string s) {
int n = s.size();
if (n<=1) return s;
string longest;
string str;
for (int i=0; i<n-1; i++) {
str = findPalindrome(s, i, i);
if (str.size() > longest.size()){
longest = str;
}
str = findPalindrome(s, i, i+1);
if (str.size() > longest.size()){
longest = str;
}
}
return longest;
}
//================================================================================
void findPalindrome(string s, int left, int right, int& start, int& len)
{
int n = s.size();
int l = left;
int r = right;
while (left>=0 && right<=n-1 && s[left] == s[right]) {
left--;
right++;
}
if (right-left-1 > len){
len = right-left-1;
start = left+1;
}
}
//The following solution is better than previous solution.
//Because it remove the sub-string return in findPalindrome().
string longestPalindrome_recursive_way2(string s) {
int n = s.size();
if (n<=1) return s;
int start=0, len=0;
string longest;
string str;
for (int i=0; i<n-1; i++) {
findPalindrome(s, i, i, start, len);
findPalindrome(s, i, i+1, start, len);
}
return s.substr(start, len);
}
//================================================================================
// Time/Memory Limit Exceeded
string longestPalindrome_dp_way(string s) {
string longest;
int n = s.size();
if (n<=1) return s;
//Construct a matrix, and consdier matrix[i][j] as s[i] -> s[j] is Palindrome or not.
//using char or int could cause the `Memory Limit Error`
//vector< vector<char> > matrix (n, vector<char>(n));
//using bool type could cause the `Time Limit Error`
vector< vector<bool> > matrix (n, vector<bool>(n));
// Dynamic Programming
// 1) if i == j, then matrix[i][j] = true;
// 2) if i != j, then matrix[i][j] = (s[i]==s[j] && matrix[i+1][j-1])
for (int i=n-1; i>=0; i--){
for (int j=i; j<n; j++){
// The following if statement can be broken to
// 1) i==j, matrix[i][j] = true
// 2) the length from i to j is 2 or 3, then, check s[i] == s[j]
// 3) the length from i to j > 3, then, check s[i]==s[j] && matrix[i+1][j-1]
if ( i==j || (s[i]==s[j] && (j-i<2 || matrix[i+1][j-1]) ) ) {
matrix[i][j] = true;
if (longest.size() < j-i+1){
longest = s.substr(i, j-i+1);
}
}
}
}
return longest;
}
// Optimized DP soltuion can be accepted by LeetCode.
string longestPalindrome_dp_opt_way(string s) {
int n = s.size();
if (n<=1) return s;
//Construct a matrix, and consdier matrix[j][i] as s[i] -> s[j] is Palindrome or not.
// ------^^^^^^
// NOTE: it's [j][i] not [i][j]
//Using vector could cause the `Time Limit Error`
//So, use the native array
bool **matrix = (bool**)malloc(n*sizeof(bool*));
int start=0, len=0;
// Dynamic Programming
// 1) if i == j, then matrix[i][j] = true;
// 2) if i != j, then matrix[i][j] = (s[i]==s[j] && matrix[i-1][j+1])
for (int i=0; i<n; i++){
matrix[i] = (bool*)malloc((i+1)*sizeof(bool));
memset(matrix[i], false, (i+1)*sizeof(bool));
matrix[i][i]=true;
for (int j=0; j<=i; j++){
// The following if statement can be broken to
// 1) j==i, matrix[i][j] = true
// 2) the length from j to i is 2 or 3, then, check s[i] == s[j]
// 3) the length from j to i > 3, then, check s[i]==s[j] && matrix[i-1][j+1]
if ( i==j || (s[j]==s[i] && (i-j<2 || matrix[i-1][j+1]) ) ) {
matrix[i][j] = true;
if (len < i-j+1){
start = j;
len = i-j+1;
}
}
}
}
for (int i=0; i<n; i++) {
free (matrix[i]);
}
free(matrix);
return s.substr(start, len);
}
string longestPalindrome(string s) {
return longestPalindrome_dp_way(s);
return longestPalindrome_dp_opt_way(s);
return longestPalindrome_recursive_way2(s);
return longestPalindrome_recursive_way(s);
}
int main(int argc, char**argv)
{
string s = "abacdfgdcaba";
if (argc > 1){
s = argv[1];
}
cout << s << " : " << longestPalindrome(s) << endl;
s = "321012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210012321001232100123210123210012321001232100123210123";
cout << s << " : " << longestPalindrome(s) << endl;
//"illi"
s = "iptmykvjanwiihepqhzupneckpzomgvzmyoybzfynybpfybngttozprjbupciuinpzryritfmyxyppxigitnemanreexcpwscvcwddnfjswgprabdggbgcillisyoskdodzlpbltefiz";
cout << s << " : " << longestPalindrome(s) << endl;
return 0;
}