-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindeRomeDP.java
More file actions
77 lines (63 loc) · 2.04 KB
/
Copy pathPalindeRomeDP.java
File metadata and controls
77 lines (63 loc) · 2.04 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package LinkedInQuestions;
/**
* Created by rmukherj on 7/29/16.
*/
class PalindeRomeDP {
int [] values;
int [][] solved;
PalindeRomeDP(int [] values) {
this.values = values;
// provide correct order of execution
solved = new int[values.length][values.length];
}
public int solve() {
// solve problems in correct order
for (int len = 2; len <= values.length; len++) {
for (int i = 0; i < values.length - len; i++) {
solve(i, i+len-1);
}
}
// solve final problem
return solve(0, values.length-1);
}
private int solve(int i, int j) {
//check if indexes cross each other
//return 1 if index overlap for else condition below
//return 0 if index i<j for condition if below
if(j == i) return 1;
if (j < i) return 0;
if (solved[i][j] > 0) return solved[i][j];
int solution;
if (values[i] == values[j]) solution = 2 + solve(i+1,j-1);
else solution = Math.max(solve(i+1, j), solve(i, j-1));
solved[i][j] = solution;
return solution;
}
/*
*/
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++; }
return R - L - 1;
}
public static void main(String[] args) {
int arr[] = new int[] {6,5,4,1,4,5,6, 2,3,4};
PalindeRomeDP pal = new PalindeRomeDP(arr);
System.out.println(pal.solve());
}
}