-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_5.java
More file actions
executable file
·41 lines (38 loc) · 901 Bytes
/
sort_5.java
File metadata and controls
executable file
·41 lines (38 loc) · 901 Bytes
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
39
40
41
package leetcode;
public class sort_5 {
public String longestPalindrome(String s) {
if(s.length()==1){
return s;
}
int maxlength=1;
String substring=null;
// check all possible sub strings
for(int i=0;i<s.length();i++){
for(int j=i+1;j<s.length();j++){
String sub=s.substring(i,j+1);
if(ispalindrome(sub)){
int length=j+1-i;
if(length>maxlength){
maxlength=length;
substring=sub;
}
}
}
}
return substring;
}
public boolean ispalindrome(String s){
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=s.charAt(s.length()-1-i)){
return false;
}
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="a";
sort_5 sort=new sort_5();
System.out.println(sort.longestPalindrome(s));
}
}