Advertisement
1988coder

516. Longest Palindromic Subsequence

Jan 4th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. /**
  4.  * Dynamic Programming (Using 2D array)
  5.  *
  6.  * Refer: 1)
  7.  * https://leetcode.com/problems/longest-palindromic-subsequence/discuss/99101/Straight-forward-Java-DP-solution
  8.  * 2)
  9.  * https://leetcode.com/problems/longest-palindromic-subsequence/discuss/99101/Straight-forward-Java-DP-solution/103147
  10.  *
  11.  * DP[i][j] = Length of the longest palindromic subsequence in substring(i, j),
  12.  * here i, j represent left, right indexes in the string.
  13.  *
  14.  * If s.charAt(i) == s.charAt(j) ==> DP[i][j] = DP[i+1][j-1] + 2. If left and
  15.  * right characters are equal, 2 plus the answer from removing both left and
  16.  * right.
  17.  *
  18.  * otherwise, DP[i][j] = Math.max(DP[i+1][j], DP[i][j-1]). Maximum of i) the
  19.  * length after removing the left edge char and ii) the length after removing
  20.  * the right edge char
  21.  *
  22.  * DP[i][i] = 1. Single char in the substring is a palindrome.
  23.  *
  24.  * Time Complexity: O(N^2)
  25.  *
  26.  * Space Complexity: O(N^2). Check below solution for O(N) space complexity.
  27.  *
  28.  * N = Length of the input string.
  29.  */
  30. class Solution1 {
  31.     public int longestPalindromeSubseq(String s) {
  32.         if (s == null) {
  33.             return 0;
  34.         }
  35.         if (s.length() < 2) {
  36.             return s.length();
  37.         }
  38.  
  39.         int strLen = s.length();
  40.         int[][] dp = new int[strLen][strLen];
  41.         for (int i = strLen - 1; i >= 0; i--) {
  42.             dp[i][i] = 1;
  43.             for (int j = i + 1; j < strLen; j++) {
  44.                 if (s.charAt(i) == s.charAt(j)) {
  45.                     dp[i][j] = dp[i + 1][j - 1] + 2;
  46.                 } else {
  47.                     dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         return dp[0][strLen - 1];
  53.     }
  54. }
  55.  
  56. /**
  57.  * Dynamic Programming (Using 1D array)
  58.  *
  59.  * Time Complexity: O(N^2)
  60.  *
  61.  * Space Complexity: O(N). Since values in DP array are depended on 2 rows. Thus
  62.  * we need two 1D arrays to store the DP information.
  63.  *
  64.  * N = Length of the input string.
  65.  */
  66. class Solution2 {
  67.     public int longestPalindromeSubseq(String s) {
  68.         if (s == null) {
  69.             return 0;
  70.         }
  71.         if (s.length() < 2) {
  72.             return s.length();
  73.         }
  74.  
  75.         int strLen = s.length();
  76.         int[] dp1 = new int[strLen];
  77.         int[] dp2 = new int[strLen];
  78.         for (int i = strLen - 1; i >= 0; i--) {
  79.             dp1[i] = 1;
  80.             for (int j = i + 1; j < strLen; j++) {
  81.                 if (s.charAt(i) == s.charAt(j)) {
  82.                     dp1[j] = dp2[j - 1] + 2;
  83.                 } else {
  84.                     dp1[j] = Math.max(dp2[j], dp1[j - 1]);
  85.                 }
  86.             }
  87.             dp2 = Arrays.copyOf(dp1, strLen);
  88.         }
  89.  
  90.         return dp1[strLen - 1];
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement