Skip to content

Commit f821262

Browse files
committed
Time: 80 ms (28.40%), Space: 74.3 MB (39.79%) - LeetHub
1 parent a8a5510 commit f821262

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int longestPalindromeSubseq(string s) {
4+
string original = s;
5+
original = " " + s;
6+
reverse(s.begin(), s.end());
7+
s = " " + s;
8+
int rows = s.length(), columns = original.length();
9+
vector<vector<int>> array(rows, vector<int>(columns, 0));
10+
for (int i = 1; i < rows; i++) {
11+
for (int j = 1; j < columns; j++) {
12+
if (s[i] == original[j]) {
13+
array[i][j] = array[i - 1][j - 1] + 1;
14+
} else {
15+
array[i][j] = max(array[i - 1][j], array[i][j - 1]);
16+
}
17+
}
18+
}
19+
return array[rows-1][columns-1];
20+
}
21+
};

0 commit comments

Comments
 (0)