Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to palindrome string with minimum insertions

In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its reverse. Therefore the number of insertions to be made is length(s) - length(lcs_string)

What method should be employed to find the equivalent palindrome string on knowing the number of insertions to be made?

For example :

1) azbzczdzez

Number of insertions required : 5 Palindrome string : azbzcezdzeczbza

Although multiple palindrome strings may exist for the same string but I want to find only one palindrome?

like image 520
whitepearl Avatar asked May 23 '12 23:05

whitepearl


People also ask

What is the minimum number of insertions required to make a string a palindrome?

Hence total insertion required is 3. We can find LCS of string using dynamic programming. Here, dynamic programming is used to calculate the length of the longest common subsequence of 'S' and the reverse of 'S'.

Which of the following problems can be used to solve the minimum number of insertions to form a palindrome problem?

6. Which of the following problems can be used to solve the minimum number of insertions to form a palindrome problem? Explanation: A variation of longest common subsequence can be used to solve the minimum number of insertions to form a palindrome problem.

Can the given string be converted to palindrome with only 1 swap allowed?

If the number of unmatched items is more than 2, it is never possible to make it a palindrome string by swapping only one character.

Can you make string palindrome?

Given a string s we need to tell minimum characters to be appended (insertion at the end) to make a string palindrome. Examples: Input : s = "abede" Output : 2 We can make string palindrome as "abedeba" by adding ba at the end of the string.


2 Answers

Let S[i, j] represents a sub-string of string S starting from index i and ending at index j (both inclusive) and c[i, j] be the optimal solution for S[i, j].

Obviously, c[i, j] = 0 if i >= j.

In general, we have the recurrence:

enter image description here

like image 84
Ravi Gupta Avatar answered Sep 30 '22 10:09

Ravi Gupta


To elaborate on VenomFangs answer, there is a simple dynamic programming solution to this one. Note that I'm assuming the only operation allowed here is insertion of characters (no deletion, updates). Let S be a string of n characters. The simple recursion function P for this is:

    = P [i+1 .. j-1], if S[i] = S[j] 

P[i..j]

    = min (P[i..j-1], P[i+1..j]) + 1,

If you'd like more explanation on why this is true, post a comment and i'd be happy to explain (though its pretty easy to see with a little thought). This, by the way, is the exact opposite of the LCS function you use, hence validating that your solution is in fact optimal. Of course its wholly possible I bungled, if so, someone do let me know!

Edit: To account for the palindrome itself, this can be easily done as follows: As stated above, P[1..n] would give you the number of insertions required to make this string a palindrome. Once the above two-dimensional array is built up, here's how you find the palindrome:

Start with i=1, j=n. Now, string output = "";

while(i < j)
{
    if (P[i][j] == P[i+1][j-1]) //this happens if no insertions were made at this point
    {
        output = output + S[i];
        i++;
        j--;
    }
    else
    if (P[i][j] == P[i+1][j]) //
    {
        output = output + S[i];
        i++;
    }
    else
    {
        output = S[j] + output;
        j--;
    }
 }
 cout<<output<<reverse(output);
 //You may have to be careful about odd sized palindromes here,
 // I haven't accounted for that, it just needs one simple check

Does that make better reading?

like image 24
kyun Avatar answered Sep 30 '22 11:09

kyun