public class LongestPalindrom {
public static void main(String args[]) {
String str = " hello . you uoy nitin nitin era woh..";
char[] a = str.toCharArray();
int low = Integer.MAX_VALUE;
int upper = Integer.MIN_VALUE;
for (int i = 0; i < str.length(); i++) {
int start = i;
int end = i;
while (start >= 0 && end < str.length()) {
if (a[start] == a[end]) {
if (end - start > upper - low) {
upper = end;
low = start;
}
end++;
start--;
} else {
break;
}
}
}
for (int i = 0; i < str.length(); i++) {
int start = i;
int end = i + 1;
while (start >= 0 && end < str.length()) {
if (a[start] == a[end]) {
if (end - start > upper - low) {
upper = end;
low = start;
}
end++;
start--;
} else {
break;
}
}
}
for (int i = low; i <= upper; i++) {
System.out.print(a[i]);
}
}
}
Output:-
LongestPalindrom
nitin nitin
0 comments :