Longest Palindromic Subsequence | DP | C # program of the above approach ; A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver program to test above function
using System ; public class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int lps ( char [ ] seq , int i , int j ) { if ( i == j ) { return 1 ; } if ( seq [ i ] == seq [ j ] && i + 1 == j ) { return 2 ; } if ( seq [ i ] == seq [ j ] ) { return lps ( seq , i + 1 , j - 1 ) + 2 ; } return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) ; } public static void Main ( ) { String seq = " GEEKSFORGEEKS " ; int n = seq . Length ; Console . Write ( " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ " + lps ( seq . ToCharArray ( ) , 0 , n - 1 ) ) ; } }