How to find the longest palindromic subsequence?

0
1180

Palindromes are the most commonly asked questions during a tech interview. Palindromes are quite appropriate, to check whether the candidate has the ability to perform different tech functions like reversing an array, forming an algorithm, and working on different aspects of a code efficiently. 

To make it even more tricky for the candidates, the interviewers often use concepts of palindrome and subsequences together in a question. 

These questions include printing the longest palindromic subsequence, printing the number of elements in the longest subsequence of a string, and others. 

Finding the longest palindromic subsequence of a given array is one such tricky question. The three concepts of palindrome, subsequence, and conditional codes are applied to resolve this type of question. 

If you are looking for a valid and efficient solution to this problem statement, you have landed on the right article. 

We have discussed various concepts of this problem statement including basic concepts of palindrome, subsequence, and the problem itself throughout this article. 

What is a palindromic Subsequence?

Reverse of a palindromic subsequence is a subsequence itself! 

The most basic explanation of a palindrome is even if you reverse the array, the array remains intact.

 For example the word “wow”. Even if you print the reverse of this string, you will get the string itself in return. 

Now, if we talk about palindromic subsequence, it is a series of characters forming a subsequence that is palindromic in nature. 

Let’s understand this concept with a working example. 

First consider the string as “BLEED”. In this string, you can form all the subsequences. Subsequences of the given string can be:

  • BL
  • LE
  • EE
  • ED
  • BE
  • BD
  • LE
  • ED

Out of these subsequences, the other subsequence with its reverse the same to the original string is “EE”. Here, ” EE” is the palindromic subsequence of the string BLEED. 

Let’s now understand what the longest palindromic subsequence is. 

What is the longest palindromic subsequence? 

In the previous section, there was only one palindromic subsequence however, if there are multiple subsequences it is important to enlist all of them and find the longest one. 

The longest palindromic subsequence is a subsequence that fulfills the criterion of being a palindrome and holds a higher number of elements than the others. 

How about we understand this concept with an example of a more complicated string? For example, the string is “BEBEEEEDD”. All the palindromic subsequences of this string are:

  • BEBE
  • EEEEE
  • EEDD
  • BBDD

Amongst the uplisted subsequences, the longest palindromic subsequence is “EEEEE”. The number of elements in this subsequence is 5 which is higher than the others in the list. 

This theoretical method can be used for shorter strings. However, for the lengthy strings, you can use two different methods to find the longest palindromic subsequence. Let’s discuss these methods in detail. 

How to find the longest Palindromic subsequence? 

There are two different methods through which you can technically print longest palindromic subsequence and the number of elements it holds. These two methods are the brute force method and the dynamic programming method. Let’s discuss the brute force method first. 

Brute force method

Do you know the method we used above is a theoretical representation of the brute force method?

 In this method, all the subsequences of a string are generated and the longest one is checked based on the count of elements. 

The total number of subsequences of a string is equal to 2 to the power of N where N is the number of elements in a string.

The following algorithm depicts how the brute force method is used to find the longest palindromic subsequence:

  • Firstly, the subsequences are checked for their first and last element. If the elements are found to be the same, it is considered a palindromic subsequence
  • In case, the first and last elements are not the same, two loops are generated. 
  • First loop will be S[i+1, j] and seccind one will be S[i+j-1]. This will provide you with the subsequences. 
  • Put a conditional parameter and check each subsequence to print the longest 

In this method, the overall time taken by the program to be executed is much more. Since all the subsequences will be generated one at a time and checked on a loop, the execution time is increased. 

You must have observed how two same subsequences are generated in a string andis checked individually. This is done only in the case of the brute force method. 

To save time and accurately print the longest palindromic subsequence of the string, you can use the dynamic programming method. 

Dynamic programming method

Dynamic programming is called the efficient approach to resolving this problem statement. In this approach, you will not have to repeat the subsequences which will bring down the execution time and also improve the overall accuracy. 

In this method, an array is considered of size N*N. Using this array, you can find the subsequences and select the longest one without repetition. 

The algorithm to explain how dynamic programming is used to find the longest palindromic subsequence comes with the following steps:

  • The first step is to check the element at the first row of this array is one or not by checking the dp[i][i]==1. 
  • Next check whether the element at the j position is the same as i Or not
  • Fill the whole array with a similar loop condition. The loop condition to be followed throughout the array is S[i]==S[j]. 
  • Finally return the value at dp[0][N-1] where you will find the longest palindromic subsequence. 

As you can notice, no subsequence is repeated for execution. Hence, the overall time condition is preferable in the dynamic programming approach. This method can be easily used on longer strings as well. 

Winding up

One of the most common questions asked during the tech interview at various companies is to print longest palindromic subsequence through an efficient method. 

You can easily follow the method of your choice to find the longest palindromic by checking each subsequence of a string depending on the overall length of your string.