-
Notifications
You must be signed in to change notification settings - Fork 22
/
Shortest Palindrome.java
executable file
·81 lines (65 loc) · 2.12 KB
/
Shortest Palindrome.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
72
73
74
75
76
77
78
79
80
81
H
1528350988
tags: String, KMP
#### Divide by mid point, Brutle
- check (mid, mid+1), or (mid-1, mid+1).
- If the two position matches, that is a palindrome candidate
- 比较front string 是否是 end string 的substring
- O(n^2)
- timeout on last case: ["aaaaaa....aaaacdaaa...aaaaaa"]
#### KMP
- TODO
```
/*
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"
*/
/*
Thoughts:
1. Find mid (based on odd or even). n / 2 or n / 2 - 1
2. loop mid -> 0: check (mid, mid+1), or (mid-1, mid+1)
3. If match, do front.reverse() check if it's substring of end
4. if so, return end.reverse() + (mid) + end
Timeout on last case: ["aaaaaa"]
*/
class Solution {
public String shortestPalindrome(String s) {
// edge case
if (s == null || s.length() <= 1) {
return s;
}
// find mid
int n = s.length();
int mid = n / 2 + (n % 2 == 0 ? -1 : 0);
// loop from mid -> 0, have buffer to store end
// perform 2 checks, and substring check
for (int i = mid; i >= 0; i--) {
String end = s.substring(i + 1);
String reversedEnd = new StringBuffer(s.substring(i + 1)).reverse().toString();
// check (mid, mid+1)
if (validateSubstring(s, i, i + 1)) {
return reversedEnd + end;
}
// check (mid-1, mid+1)
if (i >= 1 && validateSubstring(s, i - 1, i + 1)) {
return reversedEnd + s.charAt(i) + end;
}
}
return new StringBuffer(s.substring(1)).reverse().toString() + s;
}
private boolean validateSubstring(String s, int i, int j) {
String end = s.substring(j).toString();
String front = new StringBuffer(s.substring(0, i + 1)).reverse().toString();
if (front.length() <= end.length() && end.indexOf(front) == 0) {
return true;
}
return false;
}
}
```