-
Notifications
You must be signed in to change notification settings - Fork 4
/
lps.cpp
39 lines (32 loc) · 938 Bytes
/
lps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// C program of above approach
#include<bits/stdc .h>
using namespace std;
// A utility function to get max of two integers
int max (int x, int y) { return (x > y)? x : y; }
// Returns the length of the longest palindromic subsequence in seq
int lps(char *seq, int i, int j)
{
// Base Case 1: If there is only 1 character
if (i == j)
return 1;
// Base Case 2: If there are only 2
// characters and both are same
if (seq[i] == seq[j] && i 1 == j)
return 2;
// If the first and last characters match
if (seq[i] == seq[j])
return lps (seq, i 1, j-1) 2;
// If the first and last characters do not match
return max( lps(seq, i, j-1), lps(seq, i 1, j) );
}
/* Driver program to test above functions */
int main()
{
char seq[] = "GEEKSFORGEEKS";
int n = strlen(seq);
cout << "The length of the LPS is "
<< lps(seq, 0, n-1);
return 0;
}
// This code is contributed
// by Akanksha Rai