-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPalindromicSubstring.java
50 lines (44 loc) · 1.45 KB
/
LongestPalindromicSubstring.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
39
40
41
42
43
44
45
46
47
48
49
50
/* -----------------------------------
* Longest Palindromic Sequence
* -----------------------------------
*/
public class Solution {
public String longestPalindrome(String s) {
if (s == null || s.length() == 0) {
return "";
}
if(s.length() == 1){
return s;
}
int len = s.length();
String result = null;
boolean[][] dp = new boolean[len][len];
for (int i = len - 1; i >= 0; i--) {
for (int j = i; j < len; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 3 || dp[i + 1][j - 1]);
if (dp[i][j] && (result == null || j - i + 1 > result.length())) {
result = s.substring(i, j + 1);
}
}
}
return result;
}
}
public class MainClass {
public static String stringToString(String input) {
if (input == null) {
return "null";
}
return Json.value(input).toString();
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
String s = stringToString(line);
String ret = new Solution().longestPalindrome(s);
String out = (ret);
System.out.print(out);
}
}
}