How do you find the longer palindrome subsequence?

Have you brushed your knowledge on the much-asked question of the longest palindromic subsequence?

Well, this blog post is the answer to all the above questions and much more! 

During the coding stage of FAANG companies, you might be asked to find the longest palindrome subsequence. Candidates tend to get confused about what approach should be used to find it and return the output. 

Are you also wondering how to solve such questions if asked during an interview? 

In this tutorial, we will learn to find the longest palindrome subsequence from a given string “S” to return some output as an answer. 

Moreover, we will look at the concept of a burning tree and how to find the minimum time to burn a binary tree.

So, let’s get started. 

What is the Longest Palindrome Subsequence?

To find the longest palindrome subsequence, you must understand what it means and what you need to search.

Let’s understand all the keywords step-by-step.

Palindrome: Consider a string “S” given as a question to you. If the reverse string can be read the same as the forward string, it can be referred to as a palindrome. 

For example, S = ABCBAFGD

In the given string S, ABCBA is a palindrome sequence because you can read it the same from both ends.

Subsequence: When a small sequence is derived from the parent sequence by removing some elements of it, the new sequence is called a subsequence.

For example, S = ABCBAFGD

Again, consider the same string S, given in a question. If we remove some elements from this string, the remaining string can be referred to as a subsequence of the given string S.

That is, ABCBA, BCAFG, CBFGD, and BAFGD are subsequences derived from the parent string, S = ABCBAFGD.

Now, we know what we need to search when given the string “S”, we can move forward. So, let’s move a step further to learn how to find the longest palindrome subsequence.

How do you find the longest palindrome subsequence?

We should first understand “what is subsequence” before learning about the longest palindromic subsequence. It is a sequence that was created from a parent sequence as a result of the removal of an element without altering the order of the other elements. The concept of the longest common subsequence (LCS) can be used to find LPS (longest palindrome subsequence). This method is recursive. 

There is yet another approach to finding LPS efficiently. You can apply Dynamic Programming to get the length of LPS.

Sample Question: Print the length of LPS in the given string, S = “ABABCBA”.

Answer: 

Input: S = “ABABCBA”

Output: 3

Explanation: The longest common palindromic subsequence is “ABA” in the given string. It is 3 characters long and so the output is returned as 3.

Let’s try to solve the sample question using both approaches one by one.

Recursive Approach:

Here are the steps that you can follow to find LPS using a recursive approach. 

  1. Find all the possible subsequences from a given string.
  2. Pick the palindromic subsequences out of all the possible subsequences.
  3. Get the length of LPS as output and submit your answer as an integer.

Let’s see how to build the logic to find LPS from the given string.

  • Firstly, compare the first and last element of the string. To do so, you need to call a reverse of the given string. 
  • Once you get a pair of two strings (forward and reverse), compare the last elements of both strings. 
  • If both are the same, add them in the final LPS and add 2 in the output.
  1. Recurse [i + 1, j – 1]
  • If elements are different, use recursion in the following ways:
  1. Recurse S [i + 1, j]
  2. Recurse S [i, j – 1]

The recursive approach reduces the elements of the string each time. Hence, if the same letters are found, it will eliminate those letters and add them to the final result and again search for the common elements in the given string.

On the other hand, if the first and last elements of the string are not the same, there can be two possibilities.

  1. Take the forward string as it is and compare it with the reverse string (where the last element is removed). Shown as “Recurse S [i + 1, j]”.
  2. Else, take the forward string (where the last element is eliminated) and compare it with the reverse string. Shown as “Recurse S [i, j – 1]”.

The recursion process will go on until the longest common palindromic subsequences are found. The output can be converted into an integer by calling the return function.

Dynamic Programming Approach:

The dynamic programming approach considers the overlapping subsequences to find out the LPS. You can better understand it by following the example.

Consider you are given a string, S = ABCDE.

  • Let’s denote the given sequence ABCDE as LPS (0, 5).
  • The subsequences of the given string can be:
  1. ABCD – LPS (0, 4)
  2. BCDE – LPS (1, 5)
  • Furthermore, the subsequences can be written as:
  1. ABCD 2. BCDE
  1. ABC – LPS (0, 3) i) BCD – LPS (1, 4)
  2. BCD – LPS (1, 4) ii) CDE – LPS (2, 5)

The subsequence BCD is repeated twice, so we can demonstrate it with the help of a table.

  1. Draw a table and write each element of the string in columns and rows. 
  2. Give the score according to the match.
  3. Trace back to find out the LPS.

Binary Tree:

In such coding questions, you will be given a binary tree. The target node will be given, from where the burning will start. 

You will have to figure out the minimum time required or the time taken by the burning tree to burn completely.

You should keep these rules in mind while solving such questions:

  1. Only the nodes next to the target node will start to burn.
  2. Burning will spread only to the connected nodes.
  3. All the nodes of the burning tree will take the same time.
  4. Each node burns for a single time.

Winding Up:

 Here are two methods to find the longest palindrome subsequences:

  1. Using a recursive approach, and
  2. Using a dynamic programming approach.

In addition to this, you must follow the rules mentioned above while solving the questions related to the burning tree.

Leave a Comment