Havannah = Hannah

Given a sequence of characters, you are asked to find the longest palindrome within it. There is a catch : the palindrome can be built by jumping over certain letters as long as it is in order. For example for the string havannah , the hiden palindrome would be:

hava nnah = hannah \color{#D61F06} {\text{ha}} \text{va} \color{#D61F06} {\text{nnah}} = \text{hannah}

A string might have more than one such hidden palindrome. For example , alfalfa has four hidden palindromes of length 5 5 : 'alala', 'afafa', 'alfla','aflfa'

Find the length of the longest hidden palindrome for the following string .


The answer is 203.

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.

5 solutions

Thaddeus Abiy
Apr 10, 2015

The answer is merely the length of the string because its a palindrome. For anyone interested in the general problem, below is my code for finding the longest palindrome for any string . The recurrence relationship is memoized. I would love to see full blown DP solutions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def LP( L , a , b  , Memo = {}):
        #Simple Recurrence relationsip, with memoization
        Key = (a,b)
        if Key in Memo:
                return Memo[Key]
        if b - a < 0:
                Memo[Key] = ''
                return ''
        if b - a == 0:
                Memo[Key] = L[a]
                return L[a]
        if L[a] == L[b]:
                Memo[Key] = L[a] + LP( L , a + 1 , b - 1 , Memo) + L[b]
                return Memo[Key]
        Memo[Key] = max(  LP( L , a + 1 , b , Memo) , LP( L , a , b - 1 , Memo) , key = len )
        return Memo[Key]

def longestPal( L ):
    return LP( L , 0 , len(L) - 1)

Interesting solution, I was trying out substring removal, but found it a bit inefficient, so then I just checked the file and realized it was a palindrome, so I simply got its length. Nice troll ;P

David Holcer - 6 years, 2 months ago

I posted a full-blown Dynamic Programming solution :)

Arjen Vreugdenhil - 5 years, 6 months ago
Arjen Vreugdenhil
Nov 20, 2015

The answer is 203 \boxed{203} . The test string is a complete palindrome; not exactly the best testcase to see if the method is correct! Anyway, here is my code, using Dynamic Programming. (The string to be analyzed is in 'str'.)

    final int N = str.length();

    int pl[][] = new int[N][N];

    for (int i = 0; i < N; i++) pl[i][i] = 1;

    for (int L = 2; L <= N; L++)
        for (int i = 0, j = L-1; j < N; i++, j++) {
            if (str.charAt(i) == str.charAt(j)) 
                pl[i][j] = 2+(L > 2 ? pl[i+1][j-1] : 0);
            else
                pl[i][j] = Math.max(pl[i][j-1],pl[i+1][j]);
        }

    out.println(pl[0][N-1]);

Explanation: The table pl[i][j] keeps track of the longest palindrome from position i to j (inclusive). Initially, we set all pl[i][i] = 1, because strings of one character are trivially palindromes.

Now we consider successively substrings of length 2, 3, 4, and so on. Palindrome "growth" happens if this substring has the same character at the beginning and at the end. In that case, we add the two characters to the longest palindrome of the middle section. Otherwise, we copy information about the longest palindrome from the two sections of length L-1 (pick the longest).

Abdelhamid Saadi
Aug 17, 2015

This solution is solution in python 3.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
watermark = 0
def lenpal(str0, depth):
    global watermark
    res = 0  
    if len(str0) < 2 :
        res = len(str0)
    elif len(str0) == 2 :
        res = 2 if str0[0] == str0[1] else 1
    else:       
        for i in range(len(str0)-1):
            for j in range(len(str0) -1 ,i+1, - 1):
                if str0[i] == str0[j] and 2*depth + j -i > watermark:
                    res = max(res, 2 + lenpal(str0[i+1:j], depth + 1))
    watermark = max(watermark, res + 2*depth)
    return res

thestr ='79776995125591966288822764239979864294919636529599153749317118101911111012111163616102113319219811581118511891291331120161636111121011111910181171394735199592563691949246897993246722888266919552159967797'

print(lenpal(thestr, 0))

Kartik Sharma
Apr 12, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
>>> import itertools
>>> def g(n,k):
    N = str(n)
    K = []
    W = []
    for j in itertools.combinations(N,k):
        B = ''
        for t in j:
            B += t
        K.append(str(B))
    for k in K:
        a = list(k)
        if (a[::-1]==a):
            W.append(k)
    return W
>>> def t(n):
    y = len(str(n))
    while g(n,y)== []: \\as the longest length can be just the length of the string 
        y -=1
        return y

Really nice troll!

Aryan Gaikwad
Apr 12, 2015

Code worked for all my tests, but it still might be a bit buggy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
final static String n = "..."; //the number in the file
public static void main(String args[]){
    int max = 0;
    if(pal(n))
        max = n.length();
    for(int i = 1; i < n.length(); i++)
        for(int x = 0; x < i; x++)
            if(pal(remove(x,i)) && remove(x,i).length() > max)
                    max = remove(x,i).length();
    System.out.println(max);
}

private static boolean pal(String str) {
    return str.equals(new StringBuilder(str).reverse().toString());
}

private static String remove(int start, int end){
    return n.replace(n.substring(start, end), "");
}

I added a palindrome check for the original string at the start of the program just to be sure and then I realized that I am trolled.

Anyways, nice troll.

Yeah, that was a really nice troll! @Thaddeus Abiy

Kartik Sharma - 6 years, 2 months ago

Log in to reply

Yes. But unfortunately I realized it after making the algorithm. BTW, is the second longest palindrome of length 199?

Aryan Gaikwad - 6 years, 2 months ago

Log in to reply

wouldn't the second largest be 201? remove the second and second to last digit and you still have a palindrome.

David Holcer - 6 years, 2 months ago

Log in to reply

@David Holcer It is just 202 as you remove the middle digit. Length is odd.

Kartik Sharma - 6 years, 2 months ago

Log in to reply

@Kartik Sharma Oh yes. Just figured out a bug in my code.

Aryan Gaikwad - 6 years, 2 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...