Advertisement

Longest Palindromic Subsequence - LeetCode 516 Solution

Longest Palindromic Subsequence - Complete Solution Guide

Longest Palindromic Subsequence is LeetCode problem 516, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s , find the longest palindromic subsequence 's length in s . A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Example 1: Input: s = "bbbab" Output: 4 Explanation: One possible longest palindromic subsequence is "bbbb". Example 2: Input: s = "cbbd" Output: 2 Explanation: One possible longest palindromic subsequence is "bb". Constraints: 1 <= s.length <= 1000 s consists only o

Detailed Explanation

The problem asks us to find the length of the longest palindromic subsequence within a given string 's'. A subsequence is formed by deleting zero or more characters from the original string without changing the order of the remaining characters. A palindrome is a sequence that reads the same forwards and backward. The goal is to find the longest subsequence of 's' that is also a palindrome.

Solution Approach

The provided solutions use dynamic programming to solve this problem. A 2D array 'dp' is used to store the lengths of the longest palindromic subsequences for all possible substrings of the input string 's'. The 'dp' array is filled in a bottom-up manner, starting with substrings of length 1 and gradually increasing the substring length. The algorithm iterates through the string from the end to the beginning (outer loop), and for each index i, it iterates from i+1 to the end of the string (inner loop). The value of dp[i][j] is determined based on whether the characters s[i] and s[j] are equal. If they are equal, it means we can extend the palindromic subsequence found in s[i+1...j-1] by including s[i] and s[j]. If they are not equal, we take the maximum length between the longest palindromic subsequence found by excluding either s[i] or s[j]. Finally, dp[0][n-1] will hold the length of the longest palindromic subsequence of the entire string.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D array 'dp' of size n x n with all elements set to 0, where n is the length of the input string 's'.
  2. Step 2: Iterate through the string 's' from the last character to the first (i from n-1 down to 0).
  3. Step 3: For each character s[i], set dp[i][i] = 1, because a single character is a palindrome of length 1.
  4. Step 4: Iterate through the string from i+1 to the end (j from i+1 to n-1).
  5. Step 5: If s[i] == s[j], then dp[i][j] = 2 + dp[i+1][j-1]. This means the longest palindromic subsequence of s[i...j] includes both s[i] and s[j], adding 2 to the length of the longest palindromic subsequence of s[i+1...j-1].
  6. Step 6: If s[i] != s[j], then dp[i][j] = max(dp[i+1][j], dp[i][j-1]). This means the longest palindromic subsequence of s[i...j] is either the longest palindromic subsequence of s[i+1...j] or s[i...j-1], whichever is longer.
  7. Step 7: After the iterations are complete, dp[0][n-1] will contain the length of the longest palindromic subsequence of the entire string 's'.
  8. Step 8: Return dp[0][n-1].

Key Insights

  • Insight 1: The core idea is to use dynamic programming to build up solutions for smaller subproblems and combine them to solve the larger problem.
  • Insight 2: We can define a 2D DP table where dp[i][j] stores the length of the longest palindromic subsequence of the substring s[i...j].
  • Insight 3: The relationship between dp[i][j] and its subproblems is crucial: if s[i] == s[j], then dp[i][j] = 2 + dp[i+1][j-1]; otherwise, dp[i][j] = max(dp[i+1][j], dp[i][j-1]).

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n^2)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Cisco, LinkedIn, Nutanix.