-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalLong.java
38 lines (35 loc) · 1.01 KB
/
PalLong.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
class PalLong{
public static void main(String[] args) {
String str = "wpwasdfgasdfghgfdsagfdsaojd";
String pal = "";
String pal1 = "";
String longPal ="";
String longPal1 ="";
int nxt;
int max = 0;
for(int i =0 ; i < str.length()-2; i++){ /// odd palindrome
nxt= i+2;
pal1= "" +str.charAt(i+1);
pal1 = isPalindrome(str, i, nxt, pal1);
if(max<=pal1.length()){
max = pal1.length();
longPal1 = pal1;
}
//System.out.println(longPal);
pal1 = "";
}
longPal =longPal.length()>longPal1.length()?longPal:longPal1;
System.out.println(longPal);
}
/// Palindrome
public static String isPalindrome(String str, int start, int end, String pal){
if(start == 0 || end == str.length())
return str.charAt(start) + "" + pal + str.charAt(end);
else if (str.charAt(start) == str.charAt(end)) {
pal = str.charAt(start) + "" + pal + str.charAt(end);
return isPalindrome(str, start-1, end+1, pal);
}
else return pal;
}
}