A string is one of the building blocks of a programming language. Therefore, you will find string-related questions in your interviews in the majority. Yet another significant question you can be asked in an interview is to find the longest palindromic subsequence.
In this problem, you need to find a subsequence of a string that is palindromic in nature. But, mostly, you will come across solutions to find the length of the palindromic subsequence. Well, if you are asked to print longest palindromic subsequence, the method is different.
If you are confused about finding the length of the subsequence and printing the subsequence, this post is enough to clear your doubts. So, let’s check out how you can find the length of the longest palindromic subsequence and how to print it.
Before anything else, let’s understand what is a subsequence.
Understand Subsequence
A subsequence of a given sequence is derived by deleting none or multiple elements of a given sequence. However, here the order of the given characters is not manipulated in any way.
To understand the problem, consider the following example.
You are given the following sequence: ABCD
Following will be the subsequences of the given sequence: AB, AD, ABCD, C, D
Understand The Problem Statement
You will be given a sequence of characters. Here, you will have to find the subsequence which is palindromic in nature.
To understand the problem, let’s consider the following example.
You are given the following input: ABACABA
Now, the output should print ABA
Hopefully, now you have understood the given problem. Let’s now discuss how to find the longest palindromic subsequence of a given sequence.
How To Find The Longest Palindromic Subsequence?
You can employ two methods to find the longest palindromic subsequence of the given sequence. They are
- Recursive approach
- Using dynamic programming
Let’s understand both methods in detail.
Recursive Approach
The most basic approach that you can use to find the longest palindromic subsequence is a recursive approach. For this, first, find out all the possible subsequences present in the given sequence.
When done, you will have to find the longest palindromic subsequences among all the found subsequences.
You will come across a 2^N number of subsequences where n is the number of characters present in the string.
The following steps are followed in this method:
- Here, you will first have to compare the beginning and ending characters of the given sequence. In case the characters are the same, you will have to add both characters to the palindrome. Therefore, you will have to add 2 to your result. When done, your string will be recursively checked.
- In case the characters are different, it will then recurse other characters in the following ways: Recurse [i+1, j] and Recurse[i, j-1].
Dynamic Programming Method
While using the recursive method to find the longest palindromic subsequence you will have to face issues like overriding subproblems and overlapping issues. Therefore, a more efficient way is used which is dynamic programming.
The approach follows a simple method where if you come across the same subproblem, again and again, it will not be counted multiple times. In that case, that subproblem will be stored in a table.
One thing that you have to know is that these methods are used to print the length of the longest palindromic subsequence. In case you wish to print the longest palindromic subsequence, the method is different.
How To Print The Longest Palindromic Subsequence?
If you have to print the longest palindromic subsequence, the process is different and a bit tough. To print the value of the subsequence, you need to understand the longest common subsequence problem.
The common subsequence is a common subsequence between two strings that you can find without removing some or no elements of the given sequence.
For example, you are given two sequences, string 1: acbaed and string 2: abcadf
Now, the longest common subsequence will be: acad
Using the concept of the longest common subsequence, you can print the longest palindromic subsequence. The process involves the following steps:
- To begin with, find out the reverse of the given subsequence. You will have to store the reversed subsequence in the other array.
- After this, you will have to find the longest common subsequence in the reversed sequence. This common subsequence will be the longest palindromic subsequence available.
- Now that you have found the palindromic subsequence, you can simply print it.
Well, this is how you can print the longest palindromic subsequence. Now that we are talking about common interview questions, how can we not talk about the first missing positive problem? So, let’s check out what this problem is.
First Missing Positive Problem
The first missing positive problem is the best problem if you wish to learn stepwise optimization. In this problem, you will be given an array containing both negative and positive integers. Now, you may be required to find out smallest missing positive integer in a given array.
Here, let’s understand the given problem with an example.
You are given with an array of integers, A= { 1, 3, 4, -10, -9}
Now, the output will be 2.
Here, the smallest positive missing integer between 1 and 3 will be 2.
There are multiple methods that you can use to find the first missing positive integer in the given array. They are
- Use the single scan and sorting method.
- Via a hash table
- Performing in-place hashing
- The recursive method with two loops
- In-place hashing and partition.
Conclusion
To find the longest palindromic subsequence, you can use two methods including the recursive method and dynamic programming method. However, when it comes to printing the longest palindromic subsequence, the process is different.
To print the longest palindromic subsequence, you will have to learn the longest common subsequence concept.
Moreover, here we have discussed one more commonly asked question i.e, first missing positive pattern. So, do not forget to go through the following problems before you appear for your next interview.