Skip to content

Commit 2ed675b

Browse files
committed
New Problem Solution -"Longest Palindromic Subsequence"
1 parent 69fc31e commit 2ed675b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ LeetCode
151151
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [Python](./algorithms/python/K-diffPairsInAnArray/findPairs.py)|Easy|
152152
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C++](./algorithms/cpp/detectCapital/DetectCapital.cpp)|Easy|
153153
|518|[Coin Change 2](https://leetcode.com/problems/coin-change-2/) | [C++](./algorithms/cpp/coinChange/CoinChange2.cpp)|Medium|
154+
|516|[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [C++](./algorithms/cpp/longestPalindromicSubsequence/LongestPalindromicSubsequence.cpp)|Medium|
154155
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C++](./algorithms/cpp/fibonacciNumber/FibonacciNumber.cpp), [Python](./algorithms/python/FibonacciNumber/fib.py)|Easy|
155156
|497|[Random Point in Non-overlapping Rectangles](https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/) | [C++](./algorithms/cpp/randomPointInNonOverlappingRectangles/randomPointInNonOverlappingRectangles.cpp)|Medium|
156157
|494|[Target Sum](https://leetcode.com/problems/target-sum/) | [C++](./algorithms/cpp/targetSum/targetSum.cpp)|Medium|
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Source : https://leetcode.com/problems/longest-palindromic-subsequence/
2+
// Author : Hao Chen
3+
// Date : 2021-03-27
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given a string s, find the longest palindromic subsequence's length in s.
8+
*
9+
* A subsequence is a sequence that can be derived from another sequence by deleting some or no
10+
* elements without changing the order of the remaining elements.
11+
*
12+
* Example 1:
13+
*
14+
* Input: s = "bbbab"
15+
* Output: 4
16+
* Explanation: One possible longest palindromic subsequence is "bbbb".
17+
*
18+
* Example 2:
19+
*
20+
* Input: s = "cbbd"
21+
* Output: 2
22+
* Explanation: One possible longest palindromic subsequence is "bb".
23+
*
24+
* Constraints:
25+
*
26+
* 1 <= s.length <= 1000
27+
* s consists only of lowercase English letters.
28+
******************************************************************************************************/
29+
30+
/*
31+
32+
supposed s = "abbcba"
33+
34+
we can have a matrix,
35+
36+
- dp[start, end] is the longest from s[start] to s[end]
37+
38+
- if (start == end) dp[statr, end] = 1, it means every char can be palindromic
39+
40+
a b b c b a
41+
a 1 0 0 0 0 0
42+
b 0 1 0 0 0 0
43+
b 0 0 1 0 0 0
44+
c 0 0 0 1 0 0
45+
b 0 0 0 0 1 0
46+
a 0 0 0 0 0 1
47+
48+
49+
50+
calculating from the bottom to up. (Note: only care about the top-right trangle)
51+
52+
a b b c b a
53+
a 1 1 2 2 3 [5] <-- a == a , so "abbcba" comes from "bbcb" + 2
54+
b 0 1 [2] 2 3 3 <-- b == b , so "bb" comes from "" + 2
55+
b 0 0 1 1 [3] 3 <-- b == b , so "bcb" comes from "c" + 2
56+
c 0 0 0 1 1 [1] <-- c != a , so "cba" comes from max("cb", "a")
57+
b 0 0 0 0 1 [1] <-- b != a , so "ba" comes from max ("b", "a")
58+
a 0 0 0 0 0 1
59+
60+
So, we can have the following formular:
61+
62+
s[start] != s[end] ==> dp[start, end] = max (dp[start+1, end], dp[start, end-1]);
63+
s[start] == s[end] ==> dp[start, end] = dp[start+1, end-1] + 2;
64+
65+
*/
66+
67+
68+
class Solution {
69+
public:
70+
int longestPalindromeSubseq(string s) {
71+
int n = s.size();
72+
vector<vector<int>> dp(n, vector<int>(n, 0));
73+
74+
for (int start = n-1; start>=0; start--) {
75+
for (int end = start ; end < n ; end++) {
76+
if (start == end) {
77+
dp[start][end] = 1;
78+
continue;
79+
}
80+
if (s[start] == s[end]) {
81+
dp[start][end] = dp[start+1][end-1] + 2;
82+
}else{
83+
dp[start][end] = max (dp[start+1][end], dp[start][end-1]);
84+
}
85+
86+
}
87+
}
88+
return dp[0][n-1];
89+
}
90+
};

0 commit comments

Comments
 (0)