top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
1,606 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 by Hindhuja Ashokkumar Nair

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
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 :-)

2 Answers

+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
0 votes
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 by Shani Verma
Similar Questions
+8 votes

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

+2 votes

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.

+6 votes

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.
+4 votes

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.

+5 votes

Help me to write a C program which can generate list of numbers which are palindrome in decimal and octal notion. You can take some max as a limit find out all numbers which satisfy the given property between 1 to n.

...