Question

Due to mutations there could be some insertion of deletion which makes some palindromes not palindromes...

Due to mutations there could be some insertion of deletion which makes some palindromes not palindromes any more. For example, GCCACCG is a palindrome. Due to deletion, this sequence might be changed to CCACCG (ā€œGā€ was deleted).

QUESTION: design an algorithm which takes an input string and return a palindrome by inserting the smallest number of letters. You are allowed to insert characters at any position of the string. For example, AAT can be turned into palindrome TAAT by one insertion T, and GCT into GCTCG with two insertions CG. Also provide the time complexity analysis (e.g. big-O notation).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is in C

// A recursive program to find minimum
// number insertions needed to make a string
// palindrome
#include<bits/stdc++.h>
using namespace std;

// A utility function to find
// minimum of two numbers
int min(int a, int b)
{ return a < b ? a : b; }

// Recursive function to find
// minimum number of insertions
int findMinInsertions(char str[], int l, int h)
{
   // Base Cases
   if (l > h) return INT_MAX;
   if (l == h) return 0;
   if (l == h - 1) return (str[l] == str[h])? 0 : 1;

   // Check if the first and last characters are
   // same. On the basis of the comparison result,
   // decide which subrpoblem(s) to call
   return (str[l] == str[h])?
                   findMinInsertions(str, l + 1, h - 1):
                   (min(findMinInsertions(str, l, h - 1),
                   findMinInsertions(str, l + 1, h)) + 1);
}

// Driver code
int main()
{
   char str[] = "geeks";
   cout << findMinInsertions(str, 0, strlen(str) - 1);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Due to mutations there could be some insertion of deletion which makes some palindromes not palindromes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT