aboutsummaryrefslogtreecommitdiff
path: root/longest_palindromic_subseq.cpp
blob: f2a074f4b519ea8d5985aeabbd3ae979ea7d2af7 (plain)
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
// 516. Longest Palindromic Subsequence
#include "leetcode.h"

/*
 * given string 's', find the longest palindromic susbsequences
 * of length in 's'. a subsequcnes is a sequence that can be derived
 * from another sequence by deleting some or no elements without
 * changing the order of the remaining elements
 */

class Solution {
public:
  int longestPalindromeSubseq(string s) {
    int n = s.size();
    vector<int> dp(n, 0);
    for (int i = n - 1; i >= 0; i--) {
      vector<int> new_dp(n, 0);
      new_dp[i] = 1;
      for (int j = i + 1; j < n; j++) {
        if (s[i] == s[j])
          new_dp[j] = 2 + dp[j - 1];
        else
          new_dp[j] = max(dp[j], new_dp[j - 1]);
      }
      dp = new_dp;
    }
    return dp[n - 1];
  }
};

int main() {
  Solution obj;
  printf("%d\n", obj.longestPalindromeSubseq("bbbab")); // expect: 4
  printf("%d\n", obj.longestPalindromeSubseq("cbbd"));  // expect: 2
}