Convert string to palindrome string with minimum insertions [duplicate]
Asked Answered
N

4

10

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?

Ninurta answered 23/5, 2012 at 23:30 Comment(1)
Does this answer your question? how to convert a string into a palindrome with minimum number of operations?Lief
S
12

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

Strain answered 24/5, 2012 at 7:18 Comment(1)
Explain the recurrence.Weisberg
T
1

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?

Tinstone answered 24/5, 2012 at 3:37 Comment(2)
Thank you @kyun. But I successfully found out the number of insertions to be made. I have specified that I need to find the palindrome string formed after insertions? Can you give me a optimal solution? Thanking you in advance.Ninurta
"If you'd like more explanation on why this is true, post a comment"? Why don't you explain anyway, if you can, instead of baiting?Weisberg
P
-1

PHP Solution of O(n)

function insertNode(&$arr, $idx, $val) {
    $arr = array_merge(array_slice($arr, 0, $idx), array($val), array_slice($arr, $idx));
}
function createPalindrome($arr, $s, $e) {
    $i = 0;
    while(true) {
        if($s >= $e) {
            break;
        } else if($arr[$s] == $arr[$e]) {
            $s++; $e--; // shrink the queue from both sides 
            continue;
        } else {
            insertNode($arr, $s, $arr[$e]);
            $s++;
        }
    }
    echo implode("", $arr);
}
$arr = array('b', 'e', 'a', 'a', 'c', 'd', 'a', 'r', 'e');
echo createPalindrome ( $arr, 0, count ( $arr ) - 1 );
Pachysandra answered 18/10, 2016 at 9:10 Comment(0)
D
-2

Simple. See below :)

        String pattern = "abcdefghgf";
        boolean isPalindrome = false;
        int i=0,j=pattern.length()-1;
        int mismatchCounter = 0;

        while(i<=j)
        {
            //reverse matching
            if(pattern.charAt(i)== pattern.charAt(j))
                {
                    i++; j--; 
                    isPalindrome = true;
                    continue;
                }

            else if(pattern.charAt(i)!= pattern.charAt(j))
                {
                    i++;
                    mismatchCounter++;
                }


        }
        System.out.println("The pattern string is :"+pattern);
        System.out.println("Minimum number of characters required to make this string a palidnrome : "+mismatchCounter);
Display answered 16/6, 2016 at 15:54 Comment(1)
this solution won't give right answer in most of the cases. Try OROP , only 1 character is required, ie P in the beginning, but your solution will give answer 2.Vetavetch

© 2022 - 2024 — McMap. All rights reserved.