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:
ha va nnah = hannah
A string might have more than one such hidden palindrome. For example ,
alfalfa
has four hidden palindromes of length
5
:
'alala', 'afafa', 'alfla','aflfa'
Find the length of the longest hidden palindrome for the following string .
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.
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
I posted a full-blown Dynamic Programming solution :)
@Thaddeus Abiy , Here is a full blown DP Solution: http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/
The answer is 2 0 3 . 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).
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 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Really nice troll!
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 |
|
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
Log in to reply
Yes. But unfortunately I realized it after making the algorithm. BTW, is the second longest palindrome of length 199?
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.
Log in to reply
@David Holcer – It is just 202 as you remove the middle digit. Length is odd.
Log in to reply
@Kartik Sharma – Oh yes. Just figured out a bug in my code.
Problem Loading...
Note Loading...
Set Loading...
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.