For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
This article discusses in detail the concept of the Longest Palindromic Subsequence. This is a powerful tool in the world of programming, especially when working with Java and C++. It might sound complex, but to make it simple, we're going to break it down into easy-to-understand steps.
A palindrome is a sequence that reads the same way backward and forward, like "MADAM". A palindromic subsequence is a subsequence from a string, which is a palindrome. The longest palindromic subsequence is the one with the maximum length.
This algorithmic concept is useful in a lot of computing fields, including data analysis, game development, and Machine Learning. It improves the efficiency and speed of various computing operations.
In this tutorial, we'll focus on the Longest Palindromic Subsequence. It's a concept that's applicable in various programming languages, including Java, C++, and Python.
We'll teach you how to print the Longest Palindromic Subsequence. This is a fundamental step in understanding the process. We'll make it simple for you to see the output and understand how the algorithm works.
Moving on, we'll delve into the longest palindromic subsequence in Python. Python is an excellent language for learning this concept because of its easy-to-understand syntax. We'll guide you through the steps to implement this concept in Python and help you understand the underlying logic.
C++ is a robust language that offers many features for efficient coding. We'll show you how to code the Longest Palindromic Subsequence in C++ in an easy-to-follow way.
We'll wrap up with a discussion on Striver's approach to the Longest Palindromic Subsequence. Striver's method is popular, breaking down the problem into more manageable parts. It's an excellent approach for those who want to deepen their understanding of this topic.
Problem Statement 1:
Given a string, we want to find the length of the Longest Palindromic Subsequence (LPS). 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.
For example, in the string "BBABCBCAB", the longest palindromic subsequence is "BABCBAB", so the output should be 7.
Constraints:
Use the Recursive Approach.
Algorithm:
Pseudocode:
Output: "The length of the LPS is 5."
Java Output: "The length of the LPS is 7."
C++ Output: "The length of the LPS is 7"
Python Output: "The length of the LPS is 7"
Given a string, we have to find the length of the Longest Palindromic Subsequence (LPS).
For example, in the string "GEEKSFORGEEKS", the longest palindromic subsequence is "EEGEE", so the output should be 5.
Constraints:
Use the Bottom-up Dynamic Programming (DP) approach for this problem.
Algorithm:
Pseudocode:
A dry run is a step-by-step, manual execution of a code to understand its flow and verify its correctness. Here's how you would do a dry run for the Bottom-up Dynamic Programming approach using the string "GEEKS" as an example:
Here's how dp would initially look (0's indicate that these cells have not been calculated yet):
0 | 1 | 2 | 3 | 4 | |
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 |
Here's how dp would look like after this step:
0 | 1 | 2 | 3 | 4 | |
0 | 1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 0 | 0 |
2 | 0 | 0 | 1 | 0 | 0 |
3 | 0 | 0 | 0 | 1 | 0 |
4 | 0 | 0 | 0 | 0 | 1 |
Here's how dp would look like after this step:
0 | 1 | 2 | 3 | 4 | |
0 | 1 | 1 | 2 | 2 | 2 |
1 | 0 | 1 | 1 | 1 | 2 |
2 | 0 | 0 | 1 | 1 | 2 |
3 | 0 | 0 | 0 | 1 | 1 |
4 | 0 | 0 | 0 | 0 | 1 |
Java Output: "The length of the LPS is 5"
C++ Output: "The length of the LPS is 5"
Python Output: "The length of the LPS is 5"
The complexity analysis of the bottom-up dynamic programming approach for the longest palindromic subsequence problem is as follows:
Time Complexity: The time complexity is O(n^2), where ‘n’ is the length of the input string. This is because our approach is filling a 2D table of size n x n. Each cell of the table is filled in constant time, and there are n^2 cells in total, so the time complexity is O(n^2).
Space Complexity: The space complexity is also O(n^2) because we are using a 2D table of size n x n to store the intermediate results. Each cell of this table stores an integer, and there are n^2 such cells. Thus, the space required is proportional to n^2, leading to a space complexity of O(n^2).
Memoization is a powerful technique used in computer programming to optimize the execution speed of a program by storing the results of expensive function calls and reusing them when the same inputs occur again.
Here are some key functions of memoization:
In this method, we start with the naive recursive solution but add a way to store the results of overlapping subproblems to avoid recomputation.
Pseudocode:
Java Output: "The length of the LPS is 5"
C++ Output: "The length of the LPS is 5"
Output: "The length of the LPS is 5"
The Longest Palindromic Subsequence (LPS) problem involves finding the longest subsequence of a given sequence such that the subsequence is a palindrome. For instance, consider the sequence "GEEKSFORGEEKS". The longest palindromic subsequence would be "EEKEE", which has a length of 5.
The LPS problem exhibits the optimal substructure property, which means the optimal solution to the problem can be constructed from the optimal solutions of its subproblems.
Example:
Let's say we have the sequence "ABBDCACB". The LPS is "BCACB", which has a length of 5.
If we consider a subproblem by removing the first and last characters from the sequence, we get "BBDCAC". The LPS of this subsequence is also "BCACB", which is of length 5.
So, the optimal solution of the subsequence is part of the optimal solution of the main sequence. This property is referred to as the optimal substructure property.
The LPS problem also has overlapping subproblems, which means the same subproblems are solved multiple times. This happens because, while finding the LPS of the given sequence, we end up solving the same smaller problems repeatedly.
For example, while calculating the LPS for the sequence "ABBDCACB", we need to calculate the LPS for the subsequence "BBDCAC" twice, once after removing the first character and then after deleting the last character.
This overlapping of subproblems is a cue that we can improve our algorithm by using techniques like dynamic programming or memoization to store the results of subproblems and avoid solving them multiple times. This leads to a much more efficient algorithm than the naive recursive solution.
The Longest Palindromic Subsequence Problem is a classic example of a problem solvable with dynamic programming. It shows the power of this method in dealing with optimization problems, especially when the problem exhibits properties like overlapping subproblems and optimal substructure.
The bottom-up and the top-down approach with memoization provide an efficient solution, reducing the time complexity to O(n^2) from the exponential time complexity of the naive recursive solution. However, they also increase the space complexity to O(n^2) due to the use of a 2D dp table or memoization.
Printing the longest palindromic subsequence, rather than just its length, is often asked in programming challenges and interviews. It allows evaluators to see your understanding of dynamic programming concepts and your ability to manipulate and use data structures to retrieve and display information.
The longest palindromic subsequence problem shares features with other well-known dynamic programming problems like the Longest Common Subsequence (LCS) and the Longest Increasing Subsequence (LIS). They all demonstrate optimal substructure and overlapping subproblems, two key properties that make dynamic programming an ideal solution.
We use a 2D array for memoization in this problem because we need to store the results of subproblems that involve two indices . Each cell in the array stores the length of the longest palindromic subsequence for a particular subsequence of the input.
One common mistake is not handling the base cases properly, particularly the situation where a single character is a valid palindrome. Correctly setting up the memoization or dynamic programming table is a challenge.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.