Given a string, find the longest substring which is palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”.
How and what i did? I only know how to find a palindrome i.e.
Given a String.I took 2 index i,j starting from 0 and length-1.Comparing both characters present at the given index till i
Here is my code
class StringPallendrome
{
static int length = 0;
static int Count=0;
static boolean stringpallendrome(String p)
{
boolean flag=false;
boolean s3 = true;
boolean s4 = false;
char ch[] = p.toCharArray();
for(int i=0,j=ch.length-1;i<j;i++,j--)
{
if(ch[i]==ch[j])
{
flag=true;
length++;
}
else
{
flag=false;
break;
}
}
if(flag==true)
{
System.out.println("Its a pallendrome");
return s3;
}
else
{
return s4;
}
}
public static void main(String s[])
{
boolean s2;
String a = new String("abac");
s2 = stringpallendrome(a);
System.out.println(s2);
}
}
I searched various sites for this problem and found them useful but could not understand.
http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html
My domain for this problem is code in java with a simple brute force solution and then use a o(n2) approach with no extra space just like this present too. http://www.geeksforgeeks.org/longest-palindromic-substring-set-2/