   # Find the minimum no of operations to convert a string into a palindrome?

+1 vote
560 views

For eg: z is changed to y & not y to z ....the value of alphabets are changed from right to left ( eg.in the word apple ,the value of 'e' is reduced to 'd' until it becomes a palindrome or when the value becomes 'a'. posted Aug 28, 2014
Can you add some example i.e. what is the input and what is the expected output.
what I can get is one operation is from e to a (for the last character) and another is p to l (for the second character) as l to p is not allowed so number of operation is two only. But I am not clear about my understanding.
Eg: if the input is "abcd" ...we have to change  d->c then the presently changed c is changed from c->b & then again fromb->a ....then the 2nd alphabet from right is coconsidered(c) ...c->b ...so the word becones abba (palindrome) ...no of operations is 4
The value of the alphabets can be reduced by 1unit @ a time ( from e to d  ,and then d can be changed to c & so on until the alphabet is 'a' or the word is a palindrome.
Got it, first we need to find the characters which to be changed and take the sum of the diff.

Let me work on the code by tomorrow :)
:-) thank u so much Sir :-)

+1 vote
``````main()
{
char *input="apple";
int length=strlen(input);
int i,j;

int cost = 0;

for(i=0;i<length/2;i++)
{
if (input[i] != input[length-1-i])
{
if (input[i] < input[length-1-i])
{
printf("%c to be changed %c\n", input[length-1-i], input[i]);
cost += input[length-1-i] - input[i];
}
else
{
printf("%c to be changed %c\n", input[i], input[length-1-i]);
cost += input[i] - input[length-1-i];
}
}
}
printf("Total cost to make it palindrome - %d\n", cost);
}
`````` answer Aug 29, 2014 by anonymous
``````main()
{
char c[ ]="palindrom";
int i=0,l,sum=0;
l=strlen(c);
l--;
while(i<=l)
{
if(a[i]<a[l]) {
printf("Total operation to convert %c to %c is %d \n",a[l],a[i],a[l]-a[i]);
sum+=(a[l--]-a[i++]);
}
else {
printf("Total operation to convert %c to %c is %d \n",a[i],a[l],a[i]-a[l]);
sum+=(a[i++]-a[l--]);
}
}
printf("\nTotal No. of operation= %d",sum);
}
`````` answer Sep 2, 2014
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

Input: Two strings of same length (say "ababab" "bababa")
Output: Two strings with minimum unique character used in each one ("bbbbbb" & "aaaaaa" for above example)

You can swap A[i] and B[i] for all i between 1 and N, inclusive. You cannot swap two characters within A, or within B. you can only swap a character in A with the character at the same index in B, and with no other character. You can perform this operation zero or more times.

Modify the strings through the operations in such a way that the number of unique characters in the strings is minimum.

A chessboard was given to us. Where in there was a Knight and King was placed on certain positions.
Our aim is to reach the king from the knight in minimum no of counts.As we know, knight can either
move 2 steps vertical/horizontal and 1 step horizontal/vertical. same goes here as well.

Input

``````Position of Knight.
Position of King.
``````