#include<stdio.h>
#include<string.h>
int max_num(int a, int b)
{
if(a > b)
return a;
return b;
}
int lps(char *str1)
{
int i,j,len;
len = strlen(str1);
char str2[len + 1];
strcpy(str2, str1);
strrev(str2);
int arr[len + 1][len + 1];
for(i = 0; i <= len; i++)
arr[0] = 0;
for(i = 0; i <= len; i++)
arr[0] = 0;
for(i = 1; i <= len; i++)
{
for(j = 1; j <= len; j++)
{
if(str1[i-1] == str2[j - 1])
arr[j] = 1 + arr[j - 1];
else
arr[j] = max_num(arr[j], arr[j - 1]);
}
}
return arr[len][len];
}
int main()
{
char str1[] = "ababcdabba";
int ans = lps(str1);
printf("%d",ans);
return 0;
}
a) O(n)
b) O(1)
c) O(n2)
d) O(2)
What is the space complexity of the following dynamic programming implementation to find the longest palindromic subsequ
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
What is the space complexity of the following dynamic programming implementation to find the longest palindromic subsequ
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!