Longest palindrome subsequence with O(n) space
Given a sequence, find the length of the longest palindromic subsequence in it. Examples:
Input : abbaab
Output : 4
Input : geeksforgeeks
Output : 5
We have discussed a Dynamic Programming solution for Longest Palindromic Subsequence which is based on below recursive formula.
// 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
The solution discussed above takes O(n2) extra space. In this post a space optimized solution is discussed that requires O(n) extra space. The idea is to create a one dimensional array a[] of same size as given string. We make sure that a[i] stores length of longest palindromic subsequence of prefix ending with i (or substring s[0..i]).
C++
#include <bits/stdc++.h>
using namespace std;
int lps(string &s)
{
int n = s.length();
int a[n];
for ( int i = n - 1; i >= 0; i--) {
int back_up = 0;
for ( int j = i; j < n; j++) {
if (j == i)
a[j] = 1;
else if (s[i] == s[j])
{
int temp = a[j];
a[j] = back_up + 2;
back_up = temp;
}
else
{
back_up = a[j];
a[j] = max(a[j - 1], a[j]);
}
}
}
return a[n - 1];
}
int main()
{
string str = "GEEKSFORGEEKS" ;
cout << lps(str);
return 0;
}
|
Java
class GFG {
static int lps(String s)
{
int n = s.length();
int a[] = new int [n];
for ( int i = n - 1 ; i >= 0 ; i--)
{
int back_up = 0 ;
for ( int j = i; j < n; j++) {
if (j == i)
a[j] = 1 ;
else if (s.charAt(i) == s.charAt(j))
{
int temp = a[j];
a[j] = back_up + 2 ;
back_up = temp;
}
else
{
back_up = a[j];
a[j] = Math.max(a[j - 1 ], a[j]);
}
}
}
return a[n - 1 ];
}
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS" ;
System.out.println(lps(str));
}
}
|
Python3
def lps(s):
n = len (s)
a = [ 0 ] * n
for i in range (n - 1 , - 1 , - 1 ):
back_up = 0
for j in range (i, n):
if j = = i:
a[j] = 1
elif s[i] = = s[j]:
temp = a[j]
a[j] = back_up + 2
back_up = temp
else :
back_up = a[j]
a[j] = max (a[j - 1 ], a[j])
return a[n - 1 ]
string = "GEEKSFORGEEKS"
print (lps(string))
|
C#
using System;
class GFG {
static int lps( string s)
{
int n = s.Length;
int []a = new int [n];
for ( int i = n - 1; i >= 0; i--)
{
int back_up = 0;
for ( int j = i; j < n; j++) {
if (j == i)
a[j] = 1;
else if (s[i] == s[j])
{
int temp = a[j];
a[j] = back_up + 2;
back_up = temp;
}
else
{
back_up = a[j];
a[j] = Math.Max(a[j - 1], a[j]);
}
}
}
return a[n - 1];
}
public static void Main()
{
string str = "GEEKSFORGEEKS" ;
Console.WriteLine(lps(str));
}
}
|
PHP
<?php
function lps( $s )
{
$n = strlen ( $s );
for ( $i = $n - 1;
$i >= 0; $i --)
{
$back_up = 0;
for ( $j = $i ; $j < $n ; $j ++)
{
if ( $j == $i )
$a [ $j ] = 1;
else if ( $s [ $i ] == $s [ $j ])
{
$temp = $a [ $j ];
$a [ $j ] = $back_up + 2;
$back_up = $temp ;
}
else
{
$back_up = $a [ $j ];
$a [ $j ] = max( $a [ $j - 1],
$a [ $j ]);
}
}
}
return $a [ $n - 1];
}
$str = "GEEKSFORGEEKS" ;
echo lps( $str );
?>
|
Javascript
<script>
function lps(s){
let n = s.length
let a = new Array(n).fill(0);
for (let i = n - 1; i >= 0; i--){
let back_up = 0
for (let j = i; j < n; j++){
if (j == i)
a[j] = 1
else if (s[i] == s[j]){
let temp = a[j]
a[j] = back_up + 2
back_up = temp
}
else {
back_up = a[j]
a[j] = Math.max(a[j - 1], a[j])
}
}
}
return a[n - 1]
}
let string = "GEEKSFORGEEKS"
document.write(lps(string), "</br>" )
</script>
|
5
Time Complexity : O(n*n) Auxiliary Space : O(n)
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...