Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding Longest Palindrome Given a File

807589Jan 13 2009 — edited Jan 15 2009
Hello Everyone,

I am tackling an algorithmic problem trying to find the longest palindrome inside of a text file. As of right now, I know how to determine if a word is or is not a palindrome. However, the issue that I am having at the moment is trying to come up with an efficient algorithm that can solve this in the fastest way possible. Please help give ideas/suggestions on an algorithm that I should follow to achieve this.

So for example, lets say we have a text file that has 10,000 characters. One of the things that I have concluded is that in order to determine if a word is a palindrome, you must be able to know the first and last characters. Right now, the only thing that I can think of is to:
for( start with the first character x,  until the last character 10,0000)
{
          int Y = index 10,000
          while( X < Y )
          {
                if(the substring from X to the last character Y is not a palindrome)
                 {
                Y = Y - 1;
                }
                else
                {
                         currentLongestPalindrome = substring from X to Y;
                         BREAK;
                }
          }
}
Sorry everyone for the incorrect Java syntax, but that is the basic idea that I have come up with. This looks terrible as it is taking over an hour to be able to run this solution. Do you guys have any suggestions?

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 12 2009
Added on Jan 13 2009
65 comments
1,589 views