Skip to main content

Dynamic Programming

Fine Longest Palindrome Substring of a given String


class Solution {
public String longestPalindrome(String s) {
if(s==null || s.length()==0) {
return new String();
}
int i = 0;
int j = 0;
int left = 0;
int right = 0;
boolean[][] isPalindrome =
new boolean[s.length()][s.length()];
for(j=0;j<s.length();j++) {
for(i=0;i<j;i++) {
boolean isInnerPalindrome =
isPalindrome[i+1][j-1] || j-i<=2;
if(s.charAt(i)==s.charAt(j) && isInnerPalindrome) {
isPalindrome[i][j] = true;
if(j-i>right-left) {
left = i;
right = j;
}
}
}
}
return s.substring(left,right+1);
}
}

Comments

Popular posts from this blog

Running Multiple Operating Systems(Windows and Ubuntu Linux) on the same machine

VMWare Player is a freely downloadable VMWare. Download VMWare player software and install it on your windows OS download an image of the Ubuntu Linux Desktop version called Ubuntu from http://www.ubuntu.com/getubuntu/download that in iso image format. Then download VMWare configuration bundle that contains a list of files, extract those file to some folder like C:\OS\. Then edit the file" os.vmx file and give the path of the .iso image in that file in the line like below. ide1:0.fileName = C:\OS\ubuntu-8.10-desktop-i386.iso" Now open the file os.vmx file using the vmware player, that will open the Ubuntu OS. You will get a list of options in that select the option install Ubuntu without changing your current configuration of the system Now that will start the Ubuntu OS in a window inside your windows OS. Now you have a browser and all the applications inside the Ubuntu OS, you can start working on that. Double click on this window/expand it to show in full screen. To switch ...