Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Data Structure & Algorithm/minimumDeletion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 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;
}