-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathShortestPalindrome214.java
71 lines (64 loc) · 1.9 KB
/
ShortestPalindrome214.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Given a string s, you are allowed to convert it to a palindrome by adding
* characters in front of it. Find and return the shortest palindrome you can
* find by performing this transformation.
*
* Example 1:
* Input: "aacecaaa"
* Output: "aaacecaaa"
*
* Example 2:
* Input: "abcd"
* Output: "dcbabcd"
*/
public class ShortestPalindrome214 {
public String shortestPalindrome(String s) {
int len = s.length();
if (len <= 1) return s;
String res = null;
char[] chars = s.toCharArray();
for (int r=len-1; r>=0; r--) {
if (shortestPalindrome(chars, 0, r)) {
StringBuilder sb = new StringBuilder();
for (int i=len-1; i>r; i--) {
sb.append(chars[i]);
}
sb.append(chars);
return sb.toString();
}
}
return "";
}
private boolean shortestPalindrome(char[] chars, int left, int right) {
while (left < right) {
if (chars[left] != chars[right]) return false;
left++;
right--;
}
return true;
}
public String shortestPalindrome2(String s) {
int len = s.length();
if (len <= 1) return s;
String rev = new StringBuilder(s).reverse().toString();
char[] chars = (s + "#" + rev).toCharArray();
int[] prefix = prefix(chars);
return rev.substring(0, len - prefix[prefix.length - 1]) + s;
}
private int[] prefix(char[] chars) {
int len = chars.length;
int[] res = new int[len];
res[0] = 0;
for (int i=1; i<len; i++) {
int j = res[i-1];
while (j > 0 && chars[i] != chars[j]) {
j = res[j-1];
}
if (chars[i] == chars[j]) {
j++;
}
res[i] = j;
}
return res;
}
}