-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_35.java
More file actions
33 lines (27 loc) · 1.08 KB
/
Problem_35.java
File metadata and controls
33 lines (27 loc) · 1.08 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
package strings;
public class Problem_35 {
public static int minChars(String str) {
int n = str.length();
// Create a table to store longest palindrome ending at each index (LCP)
int[][] lcp = new int[n][n];
// Calculate length of longest common prefix (LCP) for all substrings
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (str.charAt(i) == str.charAt(j)) {
lcp[i][j] = (i + 1 == j) ? 1 : 1 + lcp[i + 1][j - 1];
} else {
lcp[i][j] = 0;
}
}
}
// Minimum characters to add is the length of the string minus the length of the
// longest suffix palindrome
int longestSuffixPalindrome = lcp[0][n - 1];
return n - longestSuffixPalindrome;
}
public static void main(String[] args) {
String str = "aacecaaa";
int minChars = minChars(str);
System.out.println("Minimum characters to add at front to make '" + str + "' a palindrome: " + minChars);
}
}