-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMaddy_sol
More file actions
25 lines (25 loc) · 755 Bytes
/
Copy pathMaddy_sol
File metadata and controls
25 lines (25 loc) · 755 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
class Solution {
public String longestPalindrome(String s) {
int max=0;
String answer="";
for(int i=0;i<s.length();i++){
if(i!=0 && s.charAt(i)==s.charAt(i-1)){
int cur = fun(i-2,i+1,s)+2;
if(max<cur){
max = cur;
answer = s.substring(i-cur/2,i+cur/2);
}
}
int cur = fun(i-1,i+1,s)+1;
if(max<cur){
max = cur;
answer = s.substring(i-cur/2,i+1+cur/2);
}
}
return answer;
}
private int fun(int l, int r, String s){
if(l<0 || r>=s.length() || s.charAt(l)!=s.charAt(r)) return 0;
return 2+fun(l-1,r+1,s);
}
}