Make a palindrome

What is the minimum number of characters than need to be inserted to to the string Brilliantforever to make it a palindrome?

As an explicit example:

-"ba" becomes a palindrome by adding 1 character as bab

-"lov" becomes a palindrome by adding 2 characters as lovol

Details and Assumptions

Characters can be inserted at any postion


The answer is 10.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Sean Sullivan
Aug 6, 2015

Besides a possible lone letter in the middle, the palindrome will contain two of every letter, as there is only one B , A , N , T , F , O , B, A, N, T, F, O, and V V , we will have to add at least 6 6 letters, 7 7 if none of them are directly in the middle of the palindrome.

Observing what is left if we take these lone letters out we find R I L L I R E E R RILLIREER , and we want the maximum amount of symmetry which could happen from the middle of the E E 's or the middle of the L L 's. As there are more pairs of letters surrounding the L L 's these are most symmetric and it makes most sense to make the string palindromic about the middle of them.

Looking back at the original string, these letters are already together thus there will be an even number of each letter, we must add one of all the letters that are alone 7 7 , as there are 3 3 R R s originally we must add 1 1 , and 2 2 e e 's as there are 2 2 initially on the right side of the palindromic divide. For a total of 7 + 1 + 2 = 10 7+1+2=\boxed{10} that must be added

Vishal Mittal
Jul 21, 2019

C++ Solution using Dynamic Programming

Let dp[l][r] be the minimum number of insertions to make s[l...r] a palindrome.

If s[l] = s[r], then obviously dp[l][r] = dp[l + 1][r - 1] .

Otherwise, you must either add s[r] to the beginning or s[l] to the end so dp[l][r] = 1 + min(dp[l + 1][r], dp[l][r - 1]) .

#include <bits/stdc++.h>
using namespace std;

int findMinInsertions(string &s, int i, int j, map < pair<int, int>, int > &dp)
{
    if(i >= j)
        return 0;
    if(dp.count({i,j}))
        return dp[{i, j}];

    if(s[i] == s[j])
        return dp[{i,j}] = findMinInsertions(s, i+1, j-1, dp);
    else
        return dp[{i,j}] = 1 + min(findMinInsertions(s, i+1, j, dp), findMinInsertions(s, i, j-1, dp));
}

int main()
{
    int n = 16;    //Length of string

    string s = "Brilliantforever";

    map < pair <int, int>, int > dp;

    cout << findMinInsertions(s, 0, n-1, dp); 

    return 0;
}

Problem link : SPOJ

Approach : Codeforces blog

Gobind Singh
Feb 22, 2019

Dynamic Programming Algorithm - For any string to be a palindrome, the first and the last letters must be the same. So if they are currently not the same, one would have to insert a character either at the start or at the end. If they are the same, then one can move forward.

Also for a string to be a palindrome, the string with the first and last letter are removed must also be a palindrome. So one can write the following recursive equation - Minimum steps required to make a string palindrome = Steps required to make the last two letters same + Minimum steps required to make the inner string palindrome.

Applying this to the string "bacc" Step 1: b and c not equal : Add 1 b at the end. Move to the inward string now : "acc" a and c not equal : Add 1 a at the end. Move to the inward string now : "cc": Already a palindrome.

So 2 steps were required for "bacc". One can use the same algorithm for this question to get 10 steps.

Rohit Ner
Jul 24, 2015

breveroftnailliantforeverb

Hm, why is that optimal?

Calvin Lin Staff - 5 years, 10 months ago

Log in to reply

Its supposed to be optimal. I just kept adding letters from either sides until I got the above word. I don't find any use of python.

Rohit Ner - 5 years, 10 months ago

Log in to reply

Right, so my question is "how do you know that is optimal"? Furthermore, as you said, you only "kept adding letters from either sides", whereas the problem allows us to "insert characters at any position".

You only found one way of doing it, how do you know that is indeed the best way of doing it? Are there multiple answers (of the same length)?

Calvin Lin Staff - 5 years, 10 months ago

Log in to reply

@Calvin Lin You've got a point. Actually I found so called palindromic centers of the word like "illi" and "rever". It was optimum for the case of "illi" hence I thought of it further.

Rohit Ner - 5 years, 10 months ago

I probably should have made the string longer for the use of 'python' to be evident.

But i guess calvin has pointed out what is lacking in the solution, On top of that how would you go around generalizing this optimal solution on other strings?

Beakal Tiliksew - 5 years, 10 months ago

Isn't it veoftnalibrrrbilantfoev

Suvaditya Sur - 4 years, 5 months ago

Log in to reply

You're not allowed to remove letters

Andrew Foust - 3 years, 10 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...