-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlps.cpp
More file actions
27 lines (26 loc) · 723 Bytes
/
Copy pathlps.cpp
File metadata and controls
27 lines (26 loc) · 723 Bytes
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
class Solution {
public:
int lcs(string a, string b){
vector <int> curr ( b.length()+1 , 0 );
vector <int> next ( b.length()+1 , 0 );
for(int i = a.length()-1; i >= 0; i--){
for(int j = b.length()-1; j >= 0; j--){
int len = 0;
if(a[i] == b[j]){
len = 1 + next[j+1];
}
else{
len = max(next[j], curr[j+1]);
}
curr[j] = len;
}
next = curr;
}
return next[0];
}
int longestPalindromeSubseq(string s) {
string rev = s;
reverse(rev.begin(), rev.end());
return lcs(s, rev);
}
};