• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
July 04, 2022 |940 Views

Longest palindrome subsequence with O(n) space

Description
Discussion

Every single character is a palindrome of length 1 L(i, i) = 1 for all indexes i in given sequence // IF first and last characters are not same If (X[i] != X[j]) L(i, j) = max{L(i + 1, j), L(i, j – 1)} // If there are only 2 characters and both are same Else if (j == i + 1) L(i, j) = 2 // If there are more than two characters, and first // and last characters are same Else L(i, j) = L(i + 1, j – 1) + 2

Input : abbaab
Output : 4

Input : geeksforgeeks
Output : 5

Longest palindrome subsequence with O(n) space : https://www.geeksforgeeks.org/longest-palindrome-subsequence-space/