diff --git a/Data Structure & Algorithm/minimumDeletion.c b/Data Structure & Algorithm/minimumDeletion.c new file mode 100644 index 0000000..8ec1041 --- /dev/null +++ b/Data Structure & Algorithm/minimumDeletion.c @@ -0,0 +1,40 @@ +#include +#include +#include + +// Function to calculate minimum +// of two numbers +int min(int a, int b) { + if (a < b) return a; + return b; +} + +// This function will count the minimum deletions that need +// to make a string palindrome +int minimumDeletiontoMakePalindrome(char* str, int first, int last) { + if (first >= last) return 0; + + // first and last character are same just + // increase first pointer and decrease + // last pointer and check again + if (str[first] == str[last]) + return minimumDeletiontoMakePalindrome(str, first + 1, last - 1); + + // if first and last character are not same + + // str[first] != str[last] = 1 + min(str[first+1], str[last-1]) + + // above recurrence relation is used for the recursion call + else if (str[first] != str[last]) + return 1 + min(minimumDeletiontoMakePalindrome(str, first + 1, last), + minimumDeletiontoMakePalindrome(str, first, last - 1)); +} + +int main() { + char* str = "abcdfgckba"; + int changes = minimumDeletiontoMakePalindrome(str, 0, strlen(str) - 1); + + printf("%d\n", changes); + + return 0; +} \ No newline at end of file