-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromes.java
More file actions
54 lines (47 loc) · 1.28 KB
/
Copy pathPalindromes.java
File metadata and controls
54 lines (47 loc) · 1.28 KB
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
51
52
53
54
public class Palindromes {
private static int[][] table;
public static int longestPalindromeRecursive(String s, int i, int j) {
if (i > j) {
return 0;
}
if (i == j) {
return 1;
}
if (s.charAt(i) == s.charAt(j)) {
int inside = longestPalindromeRecursive(s, i+1, j-1);
if (inside == j-1 - (i+1) + 1) {
return inside + 2;
}
}
return Math.max(longestPalindromeRecursive(s, i+1, j), longestPalindromeRecursive(s, i, j-1));
}
public static int longestPalindromeIterative(String s) {
table = new int[s.length()][s.length()];
for (int i = 1; i < s.length(); i++) {
for (int j = 0; j < i; j++) {
table[i][j] = 0;
}
}
for (int start = 0; start < s.length(); start++) {
for (int i = 0, j = start; j < s.length(); i++, j++) {
if (i == j) {
table[i][j] = 1;
} else {
if (s.charAt(i) == s.charAt(j)) {
int inside = table[i+1][j-1];
if (inside == j-1 - (i+1) + 1) {
table[i][j] = inside + 2;
continue;
}
}
table[i][j] = Math.max(table[i+1][j], table[i][j-1]);
}
}
}
return table[0][s.length()-1];
}
public static void main(String[] args) {
System.out.println(longestPalindromeRecursive("racecar", 0, 6));
System.out.println(longestPalindromeIterative("x"));
}
}