Repository navigation
Expand file tree
/
Copy pathLongestPalindrome.cpp
More file actions
65 lines (55 loc) · 2.11 KB
/
Copy pathLongestPalindrome.cpp
File metadata and controls
65 lines (55 loc) · 2.11 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
#include <string>
using namespace std;
string longestPalindrome(string s) {
// We want to limit repeating strings
// Keep track of palindromes in 2D table
// b | a | b | a | d
// b|T F T F F
// a|F T F T F
// b|T F T F F
// a|F T F T F
// d|F F F F T
int size = s.size();
if (size == 0 || size == 1) {
return s;
}
bool tab[size][size];
// Initialize table to false except for the diagonal.
for (size_t i = 0; i < size; i++) {
for (size_t j = 0; j < size; j++) {
if (i == j)
tab[i][j] = true;
else
tab[i][j] = false;
}
}
// initialize maxSize to 1 because all strings of length 1 are palindromes
int maxSize = 1;
int startIdx = 0;
// check for all palindromes length 2
for (size_t i = 0; i < size - 1; i++) {
if (s[i] == s[i+1]) {
tab[i][i+1] = true;
startIdx = i;
maxSize = 2;
}
}
// check for rest of palindromes
for (size_t i = 3; i <= size; ++i) {
for (size_t j = 0; j < size - i+1; ++j) {
// This gets the last index of the substring starting at j with length i
int k = j + i - 1;
// check if down-left item in table is a palindrome. If so, then check if comparing characters match.
// If both cases are true, then we can add a true to the table at tab[j][k]
if (tab[j + 1][k - 1] && s[j] == s[k]) {
tab[j][k] = true;
// update the startIdx and maxLength if greater than previous maxLength
if (i > maxSize) {
startIdx = j;
maxSize = i;
}
}
}
}
return s.substr(startIdx, maxSize);
}