# Can you Find number of operations to form a Palindrome?

82 views

To do this, you have to follows two rules:

1. You can reduce the value of a letter, e.g. you can change d to c, but you cannot change c to d.
2. In order to form a palindrome, if you have to repeatedly reduce the value of a letter, you can do it until the letter becomes a. Once a letter has been changed to a, it can no longer be changed.

for example: for string abc
abc->abb->aba
total 2 number of operations needed to make it a palindrome.

posted Mar 24, 2016

/*assuming c->b is one operation and b->a as second operation and assuming ahaeder files stdio.h ,string.h and stdlib.h (for abs() function)

``````int main()
{
char str[80];
int count=0,i,l,j;
gets(str);
l=strlen(str);
for(i=0,j=l-1;i<j;i++,j--)
{
count+=abs(str[i]-str[j]);
}
printf("%d",count);
return 0;
}
``````
answer Mar 28, 2016
Similar Questions

Convert the given string into palindrome with minimum number of appends(at end of the given string). O(n) algorithm will be appreciated ??

Input :=> Malayal
Output :=> Malayalam

+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.