top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can you Find number of operations to form a Palindrome?

+4 votes
409 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 by Shahsikant Dwivedi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

/*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 by Aman Mehrotra
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

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

0 votes

Given a string, add some characters to the from of it so that it becomes a palindrome.
e.g.
1) If input is "abc" then "bcabc" should be returned.
2) input -> "ab" output -> "bab"
3) input -> "a" output -> "a"

...