Page 1 of 1

What is the time complexity of the following dynamic programming implementation of the minimum number of insertions to f

Posted: Wed Jul 13, 2022 7:40 pm
by answerhappygod
#include<stdio.h>
#include<string.h>
int max(int a, int b)
{
if(a > b)
return a;
return b;
}
int min_ins(char *s)
{
int len = strlen(s), i, j;
int arr[len + 1][len + 1];
char rev[len + 1];
strcpy(rev, s);
strrev(rev);
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(s == rev[j - 1])
arr[j] = arr[j - 1] + 1;
else
arr[j] = max(arr[j], arr[j - 1]);
}
}
return len - arr[len][len];
}
int main()
{
char s[] = "abcda";
int ans = min_ins(s);
printf("%d",ans);
return 0;
}
a) O(1)
b) O(n)
c) O(n2)
d) O(mn)